4

例如我有这样的行:

iamonlywhoknock BREAKINGBAD

什么意思

'iamonlywhoknock BREAKINGBAD\n'

它的str,但我需要创建字典,如下所示:

{"iamonlywhoknock":"BREAKINGBAD"}

有任何想法吗?

4

4 回答 4

4

像这样的东西?

>>> s='iamonlywhoknock BREAKINGBAD\n'
>>> k, v = s.split()
>>> {k: v}
{'iamonlywhoknock': 'BREAKINGBAD'}
于 2012-10-20T16:32:25.277 回答
3
x='iamonlywhoknock BREAKINGBAD\n'.split(" ")
mydict={x[0]:x[1]}

这应该适合你。这是基本的字符串拆分:)

于 2012-10-20T16:32:33.107 回答
1

这篇文章中的答案与您的问题相似:

在 Python 中将分号分隔的字符串拆分为字典

但您可能希望它看起来像这样:

s= 'iamonlywhoknock BREAKINGBAD\notherwhoknock BREAKINGBAD2'
dict(item.split(" ") for item in s.split("\n"))
于 2012-10-20T16:41:09.733 回答
0
a = 'iamonlywhoknock BREAKINGBAD\n'
b = a.split()
c = {b[0]:b[1]}

print c
于 2012-10-20T16:37:05.010 回答