gpt4 book ai didi

python - file.readlines 留下空行

转载 作者:太空狗 更新时间:2023-10-30 02:04:34 30 4
gpt4 key购买 nike

<分区>

我读过 file.readlines 逐行读取整个文件并将其存储在列表中。如果我有这样的文件 -

Sentence 1
Sentence 2
Sentence 3

然后我使用 readlines 打印每个句子,就像这样 -

file = open("test.txt") 
for i in file.readlines():
print i

输出是

Sentence 1

Sentence 2

Sentence 3

我的问题是为什么每个句子之间要多出一行,我该如何去掉它?

更新

我发现使用 i.strip 也会删除多余的行。为什么会这样?据我所知,split 会删除字符串结尾和开头的空格。

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