[英]How to read each line from a file into list word by word in Python
I'm trying to read line into a list where each word on that line is different argument. 我正在尝试将行读入列表,其中该行上的每个单词都是不同的参数。 For example when my text file contains:
例如,当我的文本文件包含:
Word1, Word2, Some different words,separated by comma,but no space
Word3, Word4, Some different words,separated by comma,but no space
I would like to get lists like that: 我想要这样的清单:
['Word1', 'Word2', 'Some different words,separated by comma,but no space'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space']
Maybe I can even get list like this: 也许我什至可以得到这样的清单:
['Word1', 'Word2', 'Some different words','separated by comma', 'but no space']
So far I've managed to get this work when there is one line in text file by reading each word into list. 到目前为止,当文本文件中只有一行时,通过将每个单词读入列表,我已经设法完成了这项工作。
list_words = f.read().split()
It gives me output: 它给我输出:
['Word1', 'Word2', 'Some different words,separated by comma,but no space']
How could I do this when I have multiple lines? 当我有多行时该怎么办? Also if I later want to print out first arguments from both lists, can I use list_words[0] and it will give me automatically 'Word1' and 'Word3' ?
另外,如果以后要从两个列表中打印出第一个参数,是否可以使用list_words [0],它将自动给我'Word1'和'Word3'吗?
I hope this explanation was clear enough. 我希望这个解释很清楚。
您可以使用以下列表理解
list_words = [i.split(',') for i in f]
If you want to split with a comma followed by a space, you could use re.split
: 如果要用逗号和空格
re.split
,可以使用re.split
:
>>> with open('f.txt') as f:
... print [re.split(', ',line) for line in f]
...
[['Word1', 'Word2', 'Some different words,separated by comma,but no space\n'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space\n']]
If you want to split on every comma, just use str.split: 如果要在每个逗号上分割,请使用str.split:
>>> with open('f.txt') as f:
... print [line.split(',') for line in f]
...
[['Word1', ' Word2', ' Some different words', 'separated by comma', 'but no space\n'],
['Word3', ' Word4', ' Some different words', 'separated by comma', 'but no space\n']]
you can use strip
to get rid of the \\n
: 您可以使用
strip
摆脱\\n
:
>>> with open('f.txt') as f:
... print [line.strip().split(',') for line in f]
... # or print [re.split(', ',line.strip()) for line in f]
...
[['Word1', ' Word2', ' Some different words', 'separated by comma', 'but no space'],
['Word3', ' Word4', ' Some different words', 'separated by comma', 'but no space']]
In fact, you can also use line.strip().split(', ')
. 实际上,您也可以使用
line.strip().split(', ')
。 I just forgot that you can have a delimiter of more than 1 character… 我只是忘记了您可以使用超过1个字符的分隔符...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.