保存由列表中的制表符和逗号分隔的文本元素

时间:2018-04-04 14:57:06

标签: python python-3.x list dictionary text

我在python中有以下文本格式:

126    attr1,attr7,attr4 and attr8
1    attr6,attr2,attr9,attr78,attr23,attr56,attr75,attr77
5    attr5,attr3,attr2
7    attr0
67    attr12,attr13,attr14

所以我想将ID(126,1,5等)保存在列表中,并将每个行属性保存在列表或字典中。我使用以下代码保存了ID,但我无法保存属性。这是我的代码:

file = open("myfile.txt","r")
    lines = file.readlines()
    nodes = []
    skills = [] #or dict()
    for x in lines:
        nodes.append(x.split('\t')[0])
        skills.append(x.split(',')[0]) #i want a list of lists or a dict with attrs

3 个答案:

答案 0 :(得分:1)

我认为这会解决问题:

for x in lines:
        x = x.split('\t')
        nodes.append(x[0])
        skills.append(x[1].split(','))

答案 1 :(得分:0)

我建议使用以ids为键的单个字典,并将attr作为值列表:

d = {}
file = open("myfile.txt","r")
lines = file.readlines()   
for line in lines:
    splitted = line.split()
    d.update({splitted[0]: splitted[1].split(',')})
print(d)

# {'126': ['attr1', 'attr7', 'attr4', 'attr8'], 
#    '1': ['attr6', 'attr2', 'attr9', 'attr78', 'attr23', 'attr56', 'attr75', 'attr77'],
#    '5': ['attr5', 'attr3', 'attr2'], 
#    '7': ['attr0'], 
#   '67': ['attr12', 'attr13', 'attr14']}                   

答案 2 :(得分:0)

如果您希望所有属性都是连接字符串,那么:

attr_dict={}
with open('file.txt','r') as f:
    for line in f:
        attr_dict[line.split()[0]]=line.split()[1:]

print(attr_dict)

输出:

{'126': ['attr1,attr7,attr4', 'and', 'attr8'], '7': ['attr0'], '5': ['attr5,attr3,attr2'], '1': ['attr6,attr2,attr9,attr78,attr23,attr56,attr75,attr77'], '67': ['attr12,attr13,attr14']}

如果你想要个人元素,那么:

attr_dict={}
with open('file.txt','r') as f:
    for line in f:
        data=line.split()
        for sub_data in data:
            attr_dict[line.split()[0]]=sub_data.split(',')

print(attr_dict)

输出:

{'126': ['attr8'], '7': ['attr0'], '5': ['attr5', 'attr3', 'attr2'], '1': ['attr6', 'attr2', 'attr9', 'attr78', 'attr23', 'attr56', 'attr75', 'attr77'], '67': ['attr12', 'attr13', 'attr14']}