简体   繁体   English

如何在Python中将文件中的每一行逐字读取到列表中

[英]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.

相关问题 如何分别从文件中读取一行的每个单词? - how can i read each word of a line from a file separately? 如何用python列表中的相应元素替换文件中每行的第一个单词? - How to replace first word of each line in a file with the corresponding element from a list in python? 如何在 python 中逐字读取? - How to read line word by word in python? Python 3使用单词计数器列出行和单词从文件数组中出现的次数 - Python 3 using word counter to list line & how many times a word appears from an array in file 阅读Python中的每个单词和其余行吗? - Read each word and rest of line in Python? Python:如何逐字读取标准输入/文件? - Python: How to I read from stdin/file word by word? 在不导入任何内容的情况下,在python 3中为列表中的每个单词打印文本文件的行号 - Print the line number of a text file for each word from a list in python 3 without importing anything 从 python 中的 word 文件读取 - Read from a word file in python 如何在文件的每一行中搜索单词并在 python 中打印它的下一个值 - How to search for the word in each line of a file and print the next value of it in python 打开一个文件,将每一行拆分成一个列表,然后对每行中的每个单词检查该单词是否在列表中,如果没有将其附加到列表中 - Open a file, split each line into a list, then for each word on each line check to see if the word is in list and if not append it to the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM