繁体   English   中英

从文本文件构建字典 Python

[英]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'], 
  }
]

关键字ENDBIOEND用于表示字典应该在何处停止,不包含在最终字典中。

编辑:
到目前为止,我尝试过的最佳方法是将整个文本文件输入到列表中。 然后我尝试遍历列表以查找关键字以创建较小的列表,然后我将使用这些列表来提供字典。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM