如何在Python中逐字逐句地读取文件中的每一行

时间:2014-12-31 13:51:12

标签: python

我正在尝试将行读入列表,其中该行上的每个单词都是不同的参数。例如,当我的文本文件包含:

Word1, Word2, Some different words,separated by comma,but no space
Word3, Word4, Some different words,separated by comma,but no space

我想获得这样的列表:

['Word1', 'Word2', 'Some different words,separated by comma,but no space'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space']

也许我甚至可以得到这样的列表:

['Word1', 'Word2', 'Some different words','separated by comma', 'but no space']

到目前为止,当文本文件中有一行通过将每个单词读入列表时,我已经成功地完成了这项工作。

list_words = f.read().split()

它给了我输出:

['Word1', 'Word2', 'Some different words,separated by comma,but no space']

当我有多条线时,我怎么能这样做?另外如果我以后想要从两个列表中打印出第一个参数,我可以使用list_words [0]并自动给我“Word1”和“Word3”吗?

我希望这个解释很清楚。

2 个答案:

答案 0 :(得分:3)

您可以使用以下列表理解

list_words = [i.split(',') for i in f]

答案 1 :(得分:2)

如果您想用逗号分隔后跟空格,可以使用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']]

如果要分割每个逗号,只需使用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']]

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

事实上,您也可以使用line.strip().split(', ')。我忘了你可以有一个超过1个字符的分隔符......