python读取文本多行合并一行

# 需要合并的行数
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()
posted @ 2023-12-09 15:54  天若手提滑铲  阅读(27)  评论(0编辑  收藏  举报