[英]Build a Python Dictionary from a Text File
我有一個包含關鍵詞的文本文件。 看起來是這樣的:
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']
我現在想使用此列表創建上述字典。 這就是我被困的地方。
試試這個:
import json
#with open('sample.txt','r') as f:
# s=f.read()
s='''tomCruise
Tom Cruise
Los Angeles, CA
http://www.tomcruise.com
STARTBIO
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...
STARTBIO
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'''
ls=s.split('END\n')
res=[]
outter={}
for i in range(len(ls)):
inner={}
bio=''
follower=''
var=ls[i].split('\n')
name=var[1]
loc=var[2]
web=var[3].replace("STARTBIO",'')
for k in range(len(var)):
if "STARTBIO" in var[k]:
bio ='\n'.join(var[k:]).split('\nENDBIO')[0].replace("STARTBIO\n","")
if "ENDBIO" in var[k]:
follower='\n'.join(var[k:]).split('\nEND')[0].replace("ENDBIO\n","").split('\n')
inner['name']=name
inner['bio']=bio
inner['location']=loc
inner['web']=web
inner['following']=follower
name=var[0]
outter[name]=inner
print(json.dumps(outter, indent=4))
注意:我添加了 STARTBIO 以了解 bio 的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.