您好我有以下格式的文字。但我想转换成字典。我正在使用此代码转换为dict。但它没有用。
dict( (n,int(v)) for n,v in (a.split('=') for a in string.split() ) )
请有人帮帮我吗?感谢
name = Kurradu
image =
image_size =
producer = P. Kiran
writer =
starring = Varun Sandesh,Neha Sharma
music = Achu
cinematography = Sarvesh Murari
editing =
distributor =
released = 12 November 2009
studio =
country = IND
答案 0 :(得分:2)
几个小时前你只需一个清单,现在你需要一个字典?
text = """ name = Kurradu
image =
image_size =
producer = P. Kiran
writer =
starring = Varun Sandesh,Neha Sharma
music = Achu
cinematography = Sarvesh Murari
editing =
distributor =
released = 12 November 2009
studio =
country = IND"""
d = dict(map(str.strip, line.split('=', 1)) for line in text.splitlines())
# d is now:
{'cinematography': 'Sarvesh Murari',
'country': 'IND',
'distributor': '',
'editing': '',
'image': '',
'image_size': '',
'music': 'Achu',
'name': 'Kurradu',
'producer': 'P. Kiran',
'released': '12 November 2009',
'starring': 'Varun Sandesh,Neha Sharma',
'studio': '',
'writer': ''}
答案 1 :(得分:1)
也许你想要这样的东西:
dict(a.split('=') for a in string.split('\n'))
int(v)
不起作用,因为=
右侧的值无法转换为int