从文本文件构建Python字典

2024-09-27 07:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含关键字的文本文件。看起来是这样的:

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']

我现在想用这个列表创建上面描述的字典。这就是我被困的地方


Tags: ofthecomhttp列表wwwend文本文件
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:47

试试这个:

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的行

相关问题 更多 >

    热门问题