创建lis列表

2024-09-28 19:24:14 发布

您现在位置: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',....]

Tags: sleepstartevenpainlifesoondevelopedwouldn
3条回答

如果有制表符分隔的列,请使用^{} module读取该格式:

import csv

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

如果这些行不是制表符分隔的,只是空格分隔的,那么您需要在每一行上使用^{}(在行的空格上拆分):

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

请注意,即使制表符分隔的格式可能会给您提供单词组,这也会使您获得单独的单词。你知道吗

您可以使用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']]

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

不过,更好的选择是使用csv库或Pandas中的内容。你知道吗

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

相关问题 更多 >