Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Dictionary Python

For example I have line like this:

iamonlywhoknock BREAKINGBAD

what means

'iamonlywhoknock BREAKINGBAD\n'

Its str, but I need to create Dict, like this:

{"iamonlywhoknock":"BREAKINGBAD"}

Any ideas?

like image 439
JohnDow Avatar asked Feb 19 '23 18:02

JohnDow


2 Answers

Something like this?

>>> s='iamonlywhoknock BREAKINGBAD\n'
>>> k, v = s.split()
>>> {k: v}
{'iamonlywhoknock': 'BREAKINGBAD'}
like image 193
Chewie Avatar answered Feb 24 '23 15:02

Chewie


x='iamonlywhoknock BREAKINGBAD\n'.split(" ")
mydict={x[0]:x[1]}

This should work for you. It is basic string splitting :)

like image 26
IT Ninja Avatar answered Feb 24 '23 14:02

IT Ninja