我有一个包含关键字的文本文件。看起来是这样的:
tomCruise
Tom Cruise
Los Angeles, CA
http://www.tomcruise.com
Official TomCruise.com crew tweets. We love you guys!
Visit us at Facebook!
ENDBIO
katieH
NicoleKidman
END
PerezHilton
Perez Hilton
Hollywood, California
http://www.PerezH...
Perez Hilton is the creator and writer of one of the most famous websites
in the world. And he also loves music - a lot!
ENDBIO
tomCruise
katieH
NicoleKidman
END
我想从文本文件中创建以下格式的词典
[
'tomCruise':{
'name': 'Tom Cruise',
'bio': 'Official TomCruise.com crew tweets. We love you guys!\nVisit us at Facebook!',
'location': 'Los Angeles, CA',
'web': 'http://www.tomcruise.com',
'following': ['katieH', 'NicoleKidman'],
},
'PerezHilton':{
'name': 'Perez Hilton',
'bio': 'Perez Hilton is the creator and writer of one of the most famous websites in the world. And he also loves music - a lot!',
'location': 'Hollywood, California',
'web': 'http://www.PerezH...',
'following': ['tomCruise', 'katieH', 'NicoleKidman'],
}
]
关键词ENDBIO和END用于显示词典应停止的位置,不包括在最终词典中
编辑:
到目前为止,我尝试过的最好的方法是将整个文本文件输入到一个列表中。然后,我尝试在列表中循环查找关键字,以创建较小的列表,然后使用这些列表为字典提供信息
data = open('./data.txt')
lst = data.read().splitlines()
我得到一个列表(lst),看起来像这样
['tomCruise', 'Tom Cruise', 'Los Angeles, CA', 'http://www.tomcruise.com', 'Official TomCruise.com crew tweets. We love you guys! ', 'Visit us at Facebook!', 'ENDBIO', 'katieH', 'NicoleKidman', 'END', 'PerezHilton', 'Perez Hilton', 'Hollywood, California', 'http://www.PerezH...', 'Perez Hilton is the creator and writer of one of the most famous websites', 'in the world. And he also loves music - a lot!', 'ENDBIO', 'tomCruise', 'katieH', 'NicoleKidman', 'END', 'katieH', 'Katie Holmes', '', 'www.tomkat.com', 'ENDBIO', 'END']
我现在想用这个列表创建上面描述的字典。这就是我被困的地方
试试这个:
注:我添加STARTBIO是为了了解bio的行
相关问题 更多 >
编程相关推荐