python读取文本多行合并一行

发布时间 2023-12-09 15:58:10作者: 天若手提滑铲

合并行数

col = 4

创建新文件

nf = open("*.txt", "w+")

读取初始文件

with open("*.txt", "r") as f:
str = ""
for index, content in enumerate(f):
str += content.strip()
if (index+1) % col == 0:
# print(str)
nf.write(str + "\n")
str = ""

记得释放

nf.close()