Dan*_*sen 4 python io numpy file-handling
当我编程时,我经常使用外部软件来进行繁重的计算,但随后在Python中分析结果.这些外部软件通常是Fortran,C或C++,它们通过提供输入文件来工作.这可以是一个小文件,告诉哪个模式执行某些计算,或者它必须处理的大数据文件.这些文件通常使用某种格式(数据列之间有这么多空格).下面给出了我目前使用的数据文件的例如.
This is a header. The first line is always a header...
7352.103 26.0 2.61 -8.397 11.2
7353.510 26.0 4.73 -1.570 3.5
7356.643 26.0 5.75 -2.964 9.0
7356.648 26.0 5.35 -3.187 9.0
7364.034 26.0 5.67 -5.508 1.7
7382.523 26.0 5.61 -3.935 1.9
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否存在一个Python库来创建这样的输入文件,从阅读模板(由同事或外部软件的文档给出)?
通常我有一个NumPy格式的所有列,并希望使用模板作为示例将其提供给创建输入文件的函数.我不是在寻找一种蛮力方法,它可以很快变得难看.
我不知道在这里搜索什么,任何帮助表示赞赏.
我基本上可以复制你的样本savetxt.它的fmt变量给了我与FORTRAN代码用于读写文件相同的格式化控件.它以与FORTRAN和C打印相同的方式保留空间.
import numpy as np
example = """
This is a header. The first line is always a header...
7352.103 26.0 2.61 -8.397 11.2
...
"""
lines = example.split('\n')[1:]
header = lines[0]
data = []
for line in lines[1:]:
if len(line):
data.append([float(x) for x in line.split()])
data = np.array(data)
fmt = '%10.3f %9.1f %9.2f %9.3f %20.1f' # similar to a FORTRAN format statment
filename = 'stack21865757.txt'
with open(filename,'w') as f:
np.savetxt(f, data, fmt, header=header)
with open(filename) as f:
print f.read()
Run Code Online (Sandbox Code Playgroud)
生产:
# This is a header. The first line is always a header...
7352.103 26.0 2.61 -8.397 11.2
7353.510 26.0 4.73 -1.570 3.5
...
Run Code Online (Sandbox Code Playgroud)
编辑
这是一个将示例行转换为格式的粗略脚本:
import re
tmplt = ' 7352.103 26.0 2.61 -8.397 11.2'
def fmt_from_template(tmplt):
pat = r'( *-?\d+\.(\d+))' # one number with its decimal
fmt = []
while tmplt:
match = re.search(pat,tmplt)
if match:
x = len(match.group(1)) # length of the whole number
d = len(match.group(2)) # length of decimals
fmt += ['%%%d.%df'%(x,d)]
tmplt = tmplt[x:]
fmt = ''.join(fmt)
return fmt
print fmt_from_template(tmplt)
# %10.3f%10.1f%10.2f%10.3f%29.1f
Run Code Online (Sandbox Code Playgroud)