python - 在单个 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
解决方案
在嵌套循环后添加换行符:
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()
推荐阅读
- node.js - WebSockets:拒绝握手客户端使用的错误协议(nodejs)
- python - 无法使用 pandas 从 hdf5 文件中读取数据:AssertionError:无法解释 [void64] 的 dtype
- javascript - Module.exports 函数为变量返回“未定义”
- sql - 如何查询结果中省略的数据
- android - 如何在访问 Firebase Firestore 数据时以百分比显示进度对话框
- protractor - 我们如何在量角器黄瓜中进行异常处理并进行电子邮件通知
- python - PyQt5在numpy数组中设置RGBA像素
- zabbix - zabbix可以在没有bugzilla的情况下运行吗?zabbix 的 bugzilla 有多重要(zabbix server running Value NO)
- java - 用于 LinkedList 的 Java compareTo
- sql - SQL Server 中的可选条件