4

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?

JohnDow
  • 1,242
  • 4
  • 22
  • 40

4 Answers4

4

Something like this?

>>> s='iamonlywhoknock BREAKINGBAD\n'
>>> k, v = s.split()
>>> {k: v}
{'iamonlywhoknock': 'BREAKINGBAD'}
Chewie
  • 7,095
  • 5
  • 29
  • 36
3
x='iamonlywhoknock BREAKINGBAD\n'.split(" ")
mydict={x[0]:x[1]}

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

IT Ninja
  • 6,174
  • 10
  • 42
  • 65
1

The answer in this post is similar to your question:

Splitting a semicolon-separated string to a dictionary, in Python

But you would probably want it to look like this:

s= 'iamonlywhoknock BREAKINGBAD\notherwhoknock BREAKINGBAD2'
dict(item.split(" ") for item in s.split("\n"))
Community
  • 1
  • 1
Joshua Harris
  • 283
  • 1
  • 2
  • 9
0
a = 'iamonlywhoknock BREAKINGBAD\n'
b = a.split()
c = {b[0]:b[1]}

print c
agassi_yzh
  • 134
  • 5