python - 在 .dat 文件 python 中的特定列之间添加列
问题描述
我在目录中有大量具有不同行号和相同列号的文件。我想遍历所有文件并在特定列(4 和 5、12 和 13 之间)之间添加具有值(1.00)的新列。我想要编辑数据列并将其添加到相同的文件中。
我的数据看起来像
2.0 3.0 2.0 2.0 ...
1.0 0.0 2.0 1.0 ...
.
.
.
谢谢。
解决方案
尝试这个:
import os
dir_path = './' # path to the required directory
def add_float(val, index, dat_file):
lines = []
with open(dat_file, 'r') as rd_file:
for line in rd_file:
line = line.strip().split()
line.insert(index, str(val))
lines.append(' '.join(line))
with open(dat_file, 'w') as wrt_file:
wrt_file.write('\n'.join(lines))
for file in filter(os.path.isfile, os.listdir(dir_path)):
add_float(1.00, 3, file) # here, 1.00 is the value to be inserted after the 3rd column
推荐阅读
- assembly - mips 程序集将返回值保存到特定地址
- c - 通过文本文件数据从堆栈中添加和删除
- android - .NET Core API 向连接的应用程序发送广播?
- c# - ViewModel 通知绑定的 ComboBox List 已更改
- excel - 用VBA打开文件夹,路径中有括号
- angular - 从 Angular 服务和组件中的可观察对象返回和接收转换数据的正确方法是什么?
- graph - 如何使用散景更新networkx图的数据源?
- angular - 在 Firestore 中的 Mat 幻灯片切换真/假更新
- c++ - 我有这个程序的问题。有人看到我做错了吗?对此相当陌生
- react-native - 无法向右移动按钮