【发布时间】:2011-11-07 16:27:28
【问题描述】:
我有long text。我正在将此字符串转换为 dict。
这里是代码
data_dict = {}
filter_dict = {}
for each in text.split("\n"):
temp = each.split('=')
if len(temp) == 2:
data_dict[temp[0]] = temp[1]
data = dict((k.strip(), v.strip()) for k, v in data_dict.iteritems())
这是从文本转换为字典的输出
{'producer': 'Sailadhar Baruah',
'image': 'paporithefilm.jpg',
'distributor': '',
'alt': '',
'image size': '',
'gross': '',
'writer': 'Jahnu Barua',
'cinematography': 'Binod Pradhan',
'music': 'Satya Baruah P. P. Vidyanathan',
'followed by': '',
'narrator': '',
'director': 'Jahnu Barua',
'released': '1986',
'studio': 'Dolphin s Pvt. Ltd',
'starring': 'Gopi Desai Biju Phukan Sushil Goswami Chetana Das Dulal Roy',
'editing': '',
'name': 'Papori',
'language': 'Assamese languageAssamese',
'country': 'Assam, IND', 'budget': '',
'caption': 'A Screenshot',
'preceded by': '',
'runtime': '144 minutes'}
我只想知道我的最后一段到哪里去了?我可以将最后一段文本存储到任何变量吗?谢谢
【问题讨论】:
-
您的最后一段没有您期望的
key = value格式...或者,最后一段是否应该具有followed by的值? -
您尝试解析的文本:它是纯文本还是 XML?在您提供的 dpaste 链接上,它的语法是 XML。
-
@sberry2A 最后一段不是后面的值。这是单独的字符串。
标签: python regex dictionary