首页 > 解决方案 > 在单个 ascii 文件中每隔 N 组数据添加一个空行

问题描述

如何在从 0 到 1、1 到 2 的每次“跳跃”后添加一个空行。这是我的代码。

A =  [11,22,33,44,55,66,77,88,99]

with open ("vv.txt" ,"w") as out_file:
    for i in range(3):
        for j in  range(4):
                out_string = str(i) + " " + str(j) + " " +  str(A[i]) + " "
                out_string += "\n"
                out_file.write(out_string)
    out_file.close()

输出:

    0 0 11 
    0 1 11 
    0 2 11 
    0 3 11 
(add a blank line here)
    1 0 22 
    1 1 22 
    1 2 22 
    1 3 22 
(add a blank line here)
    2 0 33 
    2 1 33 
    2 2 33 
    2 3 33 

标签: pythonarraysascii

解决方案


在嵌套循环后添加换行符:

A =  [11,22,33,44,55,66,77,88,99]

with open ("vv.txt" ,"w") as out_file:
    for i in range(3):
        for j in  range(4):
                out_string = str(i) + " " + str(j) + " " +  str(A[i]) + " "
                out_string += "\n"
                out_file.write(out_string)
        out_file.write("\n")
    out_file.close()

推荐阅读