Python 如何在文件读取期间从每一行中去除换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18865210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to strip newlines from each line during a file read?
提问by mix
I'm reading lines from a file that contains one[*] word/line, such as:
我正在从包含一个 [*] 字/行的文件中读取行,例如:
dog
cat
person
tree
Each of these words also contains a newline \n
character. I want to read them into a list and throw away the newlines. The way I've devised is to read with readlines()
and then process the list to strip()
the newlines:
这些单词中的每一个还包含一个换行符\n
。我想将它们读入列表并丢弃换行符。我设计的方法是阅读readlines()
然后将列表处理strip()
为换行符:
with open('words.txt') as f:
words = f.readlines()
for index, word in enumerate(words):
words[index] = word.strip()
This works fine, but I can't help thinking there's a more efficient way to do this, to strip the newlines during the read process. But I can't find a way. Is there something more efficient (while also considering readability, etc.)
这工作正常,但我不禁想到有一种更有效的方法来做到这一点,在读取过程中去除换行符。但我找不到办法。有没有更有效的东西(同时还要考虑可读性等)
[*] UPDATE: I should have mentioned that some lines may contain more than one word, and in those cases however many words are on a line should go into a single list item. Both answers so far handle this (as does my own code), but I wanted to mention it.
[*] 更新:我应该提到有些行可能包含多个单词,在这种情况下,无论一行中有多少单词,都应该放入一个列表项中。到目前为止,两个答案都处理了这个问题(就像我自己的代码一样),但我想提一下。
采纳答案by Tim Pietzcker
You could use a list comprehension:
您可以使用列表理解:
with open('words.txt') as f:
words = [word.strip() for word in f]
回答by Ashwini Chaudhary
You can use map
:
您可以使用map
:
with open('words.txt') as f:
words = map(str.rstrip, f)
回答by Tim Pietzcker
You could write: lines = [s.rstrip("\n\r") for s in f.readlines()]
(notice it's not just strip
, which will do more than remove EOL characters).
您可以写:(请lines = [s.rstrip("\n\r") for s in f.readlines()]
注意strip
,这不仅仅是,它的作用不仅仅是删除 EOL 字符)。
However, if your file is large, you should maybe process each line in a loop, rather than laoding the whole file, for example as in:
但是,如果您的文件很大,您可能应该循环处理每一行,而不是加载整个文件,例如:
while True:
s = f.readline()
if s == "":
break # end of file
line = s.rstrip("\n\r")
...
回答by Graeme Stuart
For handling more than one word per line you may want to split the line.
为了每行处理一个以上的单词,您可能需要拆分该行。
with open('words.txt') as f:
result = [words.strip().split() for words in f]
This will create a list of lists, most of which are one element long. So, for example you could do this.
这将创建一个列表列表,其中大部分是一个元素长。所以,例如你可以这样做。
for words in result:
print len(words)