In [1]: txt=b"""# p* T* P* U* P*_cs U*_cs Steps dt*
...: 0.1 6.0 0.6499(6) -0.478(2) 0.6525 -0.452 30000 0.002
...: 0.2 6.0 1.442(1) -0.942(2) 1.452 -0.890 30000 0.002
...: 0.3 6.0 2.465(3) -1.376(1) 2.489 -1.298 30000 0.002"""
In [2]: txt=txt.splitlines()
txt
是一个文件替代品(PY3 中的字节串)
In [3]: data=np.genfromtxt(txt, dtype=None, names=True)
In [4]: data
Out[4]:
array([(0.1, 6.0, b'0.6499(6)', b'-0.478(2)', 0.6525, -0.452, 30000, 0.002),
(0.2, 6.0, b'1.442(1)', b'-0.942(2)', 1.452, -0.89, 30000, 0.002),
(0.3, 6.0, b'2.465(3)', b'-1.376(1)', 2.489, -1.298, 30000, 0.002)],
dtype=[('p', '<f8'), ('T', '<f8'), ('P', 'S9'), ('U', 'S9'), ('P_cs', '<f8'), ('U_cs', '<f8'), ('Steps', '<i4'), ('dt', '<f8')])
'P' 和 'U' 被加载为字符串,因为它们不能被解析为数字。
现在定义一个converter
剥离()
部分(再次使用字节串)
def rmvpar(astr):
return float(astr.split(b'(')[0])
In [9]: data=np.genfromtxt(txt, dtype=None, names=True,
converters={2:rmvpar, 3:rmvpar})
In [10]: data
Out[10]:
array([(0.1, 6.0, 0.6499, -0.478, 0.6525, -0.452, 30000, 0.002),
(0.2, 6.0, 1.442, -0.942, 1.452, -0.89, 30000, 0.002),
(0.3, 6.0, 2.465, -1.376, 2.489, -1.298, 30000, 0.002)],
dtype=[('p', '<f8'), ('T', '<f8'), ('P', '<f8'), ('U', '<f8'), ('P_cs', '<f8'), ('U_cs', '<f8'), ('Steps', '<i4'), ('dt', '<f8')])
现在这两个字段是浮点数。
但是转换器不能返回两个数字,所以我不能以这种方式保持不确定性。
另一种方法是将线条通过过滤器功能
def splt(astr):
strs=astr.split()
def foo(astr):
if b'(' in astr:
astr = astr.strip(b')').split(b'(')
return b','.join(astr)
return astr
return b','.join([foo(a) for a in strs])
In [26]: [splt(line) for line in txt]
Out[26]:
[b'#,p*,T*,P*,U*,P*_cs,U*_cs,Steps,dt*',
b'0.1,6.0,0.6499,6,-0.478,2,0.6525,-0.452,30000,0.002',
b'0.2,6.0,1.442,1,-0.942,2,1.452,-0.890,30000,0.002',
b'0.3,6.0,2.465,3,-1.376,1,2.489,-1.298,30000,0.002']
要使用它,我必须跳过标题,因为新行有两个添加的列
In [28]: data=np.genfromtxt([splt(line) for line in txt], delimiter=',',dtype=None, skip_header=1)
In [29]: data
Out[29]:
array([(0.1, 6.0, 0.6499, 6, -0.478, 2, 0.6525, -0.452, 30000, 0.002),
(0.2, 6.0, 1.442, 1, -0.942, 2, 1.452, -0.89, 30000, 0.002),
(0.3, 6.0, 2.465, 3, -1.376, 1, 2.489, -1.298, 30000, 0.002)],
dtype=[('f0', '<f8'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<i4'),
('f4', '<f8'), ('f5', '<i4'), ('f6', '<f8'), ('f7', '<f8'),
('f8', '<i4'), ('f9', '<f8')])
但我可以修改原来dtype
的 2 个字段(子)数组:
In [30]: dt=np.dtype([('p', '<f8'), ('T', '<f8'), ('P', '<f8',(2,)),
('U', '<f8',(2,)), ('P_cs', '<f8'), ('U_cs', '<f8'),
('Steps', '<i4'), ('dt', '<f8')])
In [31]: data = np.genfromtxt((splt(line) for line in txt), delimiter=',',dtype=dt, skip_header=1)
In [32]: data
Out[32]:
array([(0.1, 6.0, [0.6499, 6.0], [-0.478, 2.0], 0.6525, -0.452, 30000, 0.002),
(0.2, 6.0, [1.442, 1.0], [-0.942, 2.0], 1.452, -0.89, 30000, 0.002),
(0.3, 6.0, [2.465, 3.0], [-1.376, 1.0], 2.489, -1.298, 30000, 0.002)],
dtype=[('p', '<f8'), ('T', '<f8'), ('P', '<f8', (2,)), ('U', '<f8', (2,)),
('P_cs', '<f8'), ('U_cs', '<f8'), ('Steps', '<i4'), ('dt', '<f8')])
这样的字段看起来像:
In [33]: data['P']
Out[33]:
array([[ 0.6499, 6. ],
[ 1.442 , 1. ],
[ 2.465 , 3. ]])
dtypes
只要字段数匹配,我就可以定义 other 。
对于一个文件,而不是这些文本行,我会使用类似(未测试)的东西:
with open(filename,'wb') as f:
data = np.genfromtxt((splt(line) for line in f),...
在这里和上面,我使用生成器表达式(splt(line) for line in x)
,尽管列表理解会很好。任何打开文件并产生/返回修改后的行的代码都可以工作。