我有一个文本文件,其形状如下:
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',....]
如果有制表符分隔的列,请使用^{} module 读取该格式:
如果这些行不是制表符分隔的,只是空格分隔的,那么您需要在每一行上使用^{} (在行的空格上拆分):
请注意,即使制表符分隔的格式可能会给您提供单词组,这也会使您获得单独的单词。你知道吗
您可以使用
split()
您可能打算使用
[i.split() for i in res]
。当strip
从字符串中删除前导和尾随空格时,split
根据空格将其分隔为单独的标记。你知道吗不过,更好的选择是使用
csv
库或Pandas中的内容。你知道吗相关问题 更多 >
编程相关推荐