如何从 Python 中的文本中删除空行?
How to remove empty lines from a text in Python?
这可能是一个简单的问题,但我刚开始 Python 并且不知道为什么这不起作用。我正在尝试从文本中删除空行,但似乎没有任何效果。
我有以下示例文本
The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org
Title: Alice in Wonderland
Author: Lewis Carroll
Illustrator: Gordon Robinson
Release Date: August 12, 2006 [EBook #19033]
Language: English
Character set encoding: ASCII
*** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***
我需要的结果是一长串文本,如下所示:
The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org Title: Alice in Wonderland Author: Lewis Carroll Illustrator: Gordon Robinson Release Date: August 12, 2006 [EBook #19033] Language: English Character set encoding: ASCII *** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***
我试过了
text=open("sample.txt","r")
for line in text:
line = line.rstrip()
print(line)
和 .strip() 也是如此,但它们对文本没有任何作用。这是行不通的原因吗?我希望代码是一个单行代码或者我可以保存为变量的东西,因为我稍后需要结果。这是一个更大项目的一部分,我无法通过它。
您需要避免 print() 的默认行为,即输出换行符。您实现如下:-
with open('sample.txt') as txtfile:
for line in txtfile:
print(line.strip(), end='')
print()
对于这种特殊情况,您也可以这样做:-
with open('sample.txt') as txtfile:
contents = txtfile.read().replace('\n', '')
print(contents)
您可以将换行符替换为 spaces:
string=string.replace("\n\n"," ")
或使用 .splitlines()
,然后将它们重新组合在一起:
stringlist=string.splitlines()
string= " ".join(stringlist)
注意:在第一个例子中,我用 space 替换了每两个换行符,因为这个例子有两个换行符分隔每一行。如果每一行之间只有一个换行符,请使用“\n”。
text=open("sample.txt")
print(' '.join([line.strip() for line in text]))
你也可以使用变量来保存值并打印,而不是直接打印。
single = ' '.join([line.strip() for line in text])
print (single)
这可能是一个简单的问题,但我刚开始 Python 并且不知道为什么这不起作用。我正在尝试从文本中删除空行,但似乎没有任何效果。
我有以下示例文本
The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org
Title: Alice in Wonderland
Author: Lewis Carroll
Illustrator: Gordon Robinson
Release Date: August 12, 2006 [EBook #19033]
Language: English
Character set encoding: ASCII
*** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***
我需要的结果是一长串文本,如下所示:
The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org Title: Alice in Wonderland Author: Lewis Carroll Illustrator: Gordon Robinson Release Date: August 12, 2006 [EBook #19033] Language: English Character set encoding: ASCII *** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***
我试过了
text=open("sample.txt","r")
for line in text:
line = line.rstrip()
print(line)
和 .strip() 也是如此,但它们对文本没有任何作用。这是行不通的原因吗?我希望代码是一个单行代码或者我可以保存为变量的东西,因为我稍后需要结果。这是一个更大项目的一部分,我无法通过它。
您需要避免 print() 的默认行为,即输出换行符。您实现如下:-
with open('sample.txt') as txtfile:
for line in txtfile:
print(line.strip(), end='')
print()
对于这种特殊情况,您也可以这样做:-
with open('sample.txt') as txtfile:
contents = txtfile.read().replace('\n', '')
print(contents)
您可以将换行符替换为 spaces:
string=string.replace("\n\n"," ")
或使用 .splitlines()
,然后将它们重新组合在一起:
stringlist=string.splitlines()
string= " ".join(stringlist)
注意:在第一个例子中,我用 space 替换了每两个换行符,因为这个例子有两个换行符分隔每一行。如果每一行之间只有一个换行符,请使用“\n”。
text=open("sample.txt")
print(' '.join([line.strip() for line in text]))
你也可以使用变量来保存值并打印,而不是直接打印。
single = ' '.join([line.strip() for line in text])
print (single)