我有一个文本文件foo.txt
,
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
当我使用python读取此内容时,
with open("foo.txt", "r+") as f:
lines = f.readlines()
for line in lines:
print(line)
然后我的输出是这样的
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
如何摆脱多余的\n
?
谢谢
答案 0 :(得分:0)
您可以使用replace将"\n"
替换为文本中的""
。
with open("foo.txt", "r+") as f:
lines = f.readlines()
for line in lines:
line.replace("\n","")
print(line)
或可以使用end
取消打印中的自动\n
。
with open("foo.txt", "r+") as f:
lines = f.readlines()
for line in lines:
print(line,end="")
第二个可能就是您想要的。