例如我有这样的行:
iamonlywhoknock BREAKINGBAD
什么意思
'iamonlywhoknock BREAKINGBAD\n'
它的str,但我需要创建字典,如下所示:
{"iamonlywhoknock":"BREAKINGBAD"}
有任何想法吗?
例如我有这样的行:
iamonlywhoknock BREAKINGBAD
什么意思
'iamonlywhoknock BREAKINGBAD\n'
它的str,但我需要创建字典,如下所示:
{"iamonlywhoknock":"BREAKINGBAD"}
有任何想法吗?
像这样的东西?
>>> s='iamonlywhoknock BREAKINGBAD\n'
>>> k, v = s.split()
>>> {k: v}
{'iamonlywhoknock': 'BREAKINGBAD'}
x='iamonlywhoknock BREAKINGBAD\n'.split(" ")
mydict={x[0]:x[1]}
这应该适合你。这是基本的字符串拆分:)
这篇文章中的答案与您的问题相似:
但您可能希望它看起来像这样:
s= 'iamonlywhoknock BREAKINGBAD\notherwhoknock BREAKINGBAD2'
dict(item.split(" ") for item in s.split("\n"))
a = 'iamonlywhoknock BREAKINGBAD\n'
b = a.split()
c = {b[0]:b[1]}
print c