gpt4 book ai didi

Python 合并没有空白新行的行

转载 作者:行者123 更新时间:2023-12-04 02:36:55 24 4
gpt4 key购买 nike

我需要你的帮助来解决以下问题。例如,我有一些大文本文件:

This is the Name of the Person

This is his surname

He likes to sing
every time.

我只想合并行 He likes to singevery time. 因为我在这之后用每个字符串做其他正则表达式的东西。

所以输出应该是:

This is the Name of the Person

This is his surname

He likes to sing every time.

所以我试过:

for file in file_list:
with open(file, 'r', encoding='UTF-8', errors='ignore') as f_in:
for line in f_in:
if not line.startswith('\n'):
line.replace('\n', '')
print(line)

感谢您的帮助。

最佳答案

你可以试试这个:

for file in file_list:
with open(file, 'r', encoding='UTF-8', errors='ignore') as f_in:
lines = [i.replace('\n', ' ') for i in f_in.read().split('\n\n')]

# here you do something with your `lines`

关于Python 合并没有空白新行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61338038/

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