gpt4 book ai didi

python - 删除txt文件中的换行符

转载 作者:行者123 更新时间:2023-11-30 23:20:09 25 4
gpt4 key购买 nike

如果我想从文本文件中删除换行符,如下所示:

hello

there

我使用这样的简单代码:

with open('words.txt') as text:
for line in text:
print (line.strip())

它输出:

hello

there

但是,我希望我的代码输出以下内容:

hello
there

我该怎么做?提前致谢。

最佳答案

print 语句之前添加 if line.strip() == '': continue

关于python - 删除txt文件中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25684209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com