python中从文本文件读入两个列表的数据

python中从文本文件读入两个列表的数据,python,Python,我的文本文件格式为: apple very healthy orange tangy and juicy banana yellow in color and yummy 我需要创建两个列表之一: l1 = ['apple','orange','banana'] l2=['very healthy','tangy and juicy','yellow in color and yummy'] 或将值转换为字典: d1={'apple':'very healthy',

我的文本文件格式为:

apple      very healthy
orange     tangy and juicy
banana     yellow in color and yummy
我需要创建两个列表之一:

l1 = ['apple','orange','banana']
l2=['very healthy','tangy and juicy','yellow in color and yummy']
或将值转换为字典:

d1={'apple':'very healthy','orange':'tangy and juicy','banana':'yellow in color and yummy'}
l1=[]
l2=[]
d={}
read_file=open('edges.txt','r')
split= [line.strip() for line in read_file]
for line in split:
    l1.append(line.split('\t')[0])
    l2.append(line.split('\t')[1:])
d=dict(zip(l1,l2))
print d
文件中的前两列由tab分隔

我尝试使用以下代码将其更改为两个列表,然后将其转换为字典:

d1={'apple':'very healthy','orange':'tangy and juicy','banana':'yellow in color and yummy'}
l1=[]
l2=[]
d={}
read_file=open('edges.txt','r')
split= [line.strip() for line in read_file]
for line in split:
    l1.append(line.split('\t')[0])
    l2.append(line.split('\t')[1:])
d=dict(zip(l1,l2))
print d

我得到了一些不正确的值。我是python的新手。

问题可能是文件的列实际上不是由制表符分隔的,而是由多个空格分隔的(事实上,您发布的“文本文件格式”不使用制表符)。解决此问题的一种方法是:

l1=[]
l2=[]
d={}
read_file=open('edges.txt','r')
split= [line.strip() for line in read_file]
for line in split:
    l1.append(line.split('  ')[0].strip())
    l2.append('  '.join(line.split('  ')[1:]).strip())
d=dict(zip(l1,l2))
print d
如果至少使用了两个空格,则这将分隔两列。但是,如果您实际使用的是选项卡,这将不起作用,在这种情况下,您应该使用原始代码。 并且,如果没有一个值(例如,
tangy And juicy
very Health
)在一行中有两个空格,则可以替换

'  '.join(line.split('  ')[1:]).strip()

line.split('\t')
返回一个列表,
line.split('\t')[0]
返回该列表的第一个元素(“苹果”、“橙色”、“香蕉”)

l2.append(line.split('\t')[1://code>返回一个列表,因为
[1://code>是一个。也许您想改为
l2.append(line.split('\t')[1]

我忍不住重写了代码:

d={}
for line in open('edges.txt','r'):
    split = line.strip().split('\t', 1)
    d[split[0]] = split[1]
print d
进口稀土

d = {}
with open('data') as f:
    for line in f:
        mobj =  re.match('(\w+)\s+(.*)',line)
        key, value = mobj.groups()
        d[key] = value


for k,v in d.items():
    print(k,"   ", v )
输出

香蕉黄的颜色和美味

苹果非常健康


橙色浓烈多汁

确保您的文本文件包含这些值之间的选项卡,我从这里复制的内容有空格

文本文件:

apple   very healthy
orange  tangy and juicy
banana  yellow in color and yummy
脚本的输出:

{'orange':['tangy and juicy'],'apple':['very Health'],'banana':['yellow in color and yummy']}


如果您的文本文件实际上是固定宽度的(即包含空格而不是制表符),则只需使用索引对前10个字符(作为字典中的键)和第11个字符(作为值)进行切片即可对其进行解析


有一些关于解析更复杂的固定宽度文本文件的答案;您也可以使用。

否在这里它们不是固定宽度否在这里文本文件有两列,它们由制表符分隔。但是第二列不是单个单词。它是一个语句或多个单词。此代码不用于使用一个分隔列空格,用于用2个或更多空格分隔列。此外,您应该尝试此代码,因为在文本文件的示例中,列之间用5-6个空格分隔,而不是制表符。