创建列表列表

时间:2017-05-04 06:22:40

标签: python

我有一个文本文件,其形状如下:

0.6486020643999225      staunch wouldn  grew    even resisting
1.0     tinge loneliness        soon start      life life
0.6486020643999225      staunch wouldn  grew    resisting
1.0     pain    piercing        consciousness pain familiar except ten times attack
1.0     two ribs        developed       fissure
0.5073402520621506      mission freeing parents
1.0     my ribs developed       fissure
1.0     drive   wanted at_time  same night
1.0     heavy drifted sleep     fred    shoulder sleep baby
1.0     loneliness      start   life life
1.0     loneliness      soon start      new life life
1.0     pain    piercing        consciousness pain familiar except ten times raging attack
1.0     furious could break     two teeth
1.0     loneliness      start   new life life
0.8491130556422606      loneliness      attached        mission

我需要输出

[[0.6486020643999225,'staunch', 'wouldn',  'grew', 'even','resisting'],
 [1.0,     'tinge', 'loneliness' , 'soon',' start','life', 'life'],
 ...
]

我写的代码是

res=[]
with open("myfile.txt") as f:
    for i in f:
       #print(line)
        res.append(i)
user=[(i.strip()) for i in res]
print(user)

我得到的输出是:

['0.6486020643999225      staunch wouldn  grew    even resisting', '1.0     tinge loneliness        soon start      life life', '0.6486020643999225      staunch wouldn  grew    resisting', '1.0     pain    piercing        consciousness pain familiar except ten times attack', '1.0     two ribs        developed       fissure', '0.5073402520621506      mission freeing parents', '1.0     my ribs developed       fissure', '1.0     drive   wanted at_time  same night', '1.0     heavy drifted sleep     fred    shoulder sleep baby', '1.0     loneliness      start   life life',....]

3 个答案:

答案 0 :(得分:0)

您似乎有制表符分隔的列,请使用csv module来读取该格式:

import csv

with open("myfile.txt") as f:
    reader = csv.reader(f, delimiter='\t')
    user = list(reader)

如果这些行不是以制表符分隔的,而只是以空格分隔,那么您希望在每一行上使用str.split()(在

with open("myfile.txt") as f:
    user = [line.split() for line in f]

请注意,即使制表符分隔的格式可能会为您提供单词组,这也会为您提供单独的单词。

答案 1 :(得分:0)

您可能打算使用[i.split() for i in res]。当strip从字符串中删除前导和尾随空格时,split会根据空格将其分隔为单独的标记。

但更好的选择是使用来自csv库或Pandas的内容。

import pandas as pd
data = pd.read_table("myfile.txt")

答案 2 :(得分:0)

您可以使用split()

with open("myfile.txt") as f:
    print([i.split() for i in f])

[['0.6486020643999225', 'staunch', 'wouldn', 'grew', 'even', 'resisting'], ['1.0', 'tinge', 'loneliness', 'soon', 'start', 'life', 'life'], ['0.6486020643999225', 'staunch', 'wouldn', 'grew', 'resisting'], ['1.0', 'pain', 'piercing', 'consciousness', 'pain', 'familiar', 'except', 'ten', 'times', 'attack'], ['1.0', 'two', 'ribs', 'developed', 'fissure'], ['0.5073402520621506', 'mission', 'freeing', 'parents'], ['1.0', 'my', 'ribs', 'developed', 'fissure'], ['1.0', 'drive', 'wanted', 'at_time', 'same', 'night'], ['1.0', 'heavy', 'drifted', 'sleep', 'fred', 'shoulder', 'sleep', 'baby'], ['1.0', 'loneliness', 'start', 'life', 'life'], ['1.0', 'loneliness', 'soon', 'start', 'new', 'life', 'life'], ['1.0', 'pain', 'piercing', 'consciousness', 'pain', 'familiar', 'except', 'ten', 'times', 'raging', 'attack'], ['1.0', 'furious', 'could', 'break', 'two', 'teeth'], ['1.0', 'loneliness', 'start', 'new', 'life', 'life'], ['0.8491130556422606', 'loneliness', 'attached', 'mission']]