gpt4 book ai didi

python - genfromtxt 返回 -1 和 nan 行

转载 作者:行者123 更新时间:2023-12-01 02:45:47 24 4
gpt4 key购买 nike

我正在尝试使用 numpy 读取 txt 文件,并且我有以下代码

import numpy as np
def parsefile(filename):
return np.genfromtxt(filename,
delimiter = dly_delimiter,
usecols = dly_usecols,
dtype = dly_dtype,
skip_header = 4,
names = dly_names)
dly_delimiter = [7,5,9,13,10,13,9,13,10,13,10]
dly_usecols = [1,7]
dly_dtype = [np.int32, np.float64]
dly_names = ['node number', 'z_force']
force = parsefile('nodfor')
print(force)

结果如下

[(-1, nan) (-1, nan) (-1, nan) ..., (-1, nan) (-1, nan) (-1, nan)]

但我感兴趣的是“nd#”后面的数字和“zforce”后面的数字。

[the format of the txt file is like this]这是 txt 文件的片段(两行):

 nd#   39584  xforce=   0.0000E+00   yforce=   0.0000E+00  zforce=   
0.0000E+00 energy= 0.0000E+00 setid = 1
nd# 39585 xforce= 0.0000E+00 yforce= 0.0000E+00 zforce=
0.0000E+00 energy= 0.0000E+00 setid = 1

这是一个已知问题吗?我该如何解决这个问题?

最佳答案

由于注释字符,我的第一次尝试错过了大部分文本:

In [44]: txt = b""" nd#   39584  xforce=   0.0000E+00   yforce=   0.0000E+00  zforce=   0.0000E+00   energy=   0.0000E+00   setid =       1
...: nd# 39585 xforce= 0.0000E+00 yforce= 0.0000E+00 zforce= 0.0000E+00 energy= 0.0000E+00 setid = 1
...: """
In [45]: data=np.genfromtxt(txt.splitlines(),dtype=None)
In [46]: data
Out[46]:
array([b'nd', b'nd'],
dtype='|S2')

关闭评论:

In [53]: data=np.genfromtxt(txt.splitlines(),dtype=None, comments=None)
In [54]: data
Out[54]:
array([ (b'nd#', 39584, b'xforce=', 0., b'yforce=', 0., b'zforce=', 0., b'energy=', 0., b'setid', b'=', 1),
(b'nd#', 39585, b'xforce=', 0., b'yforce=', 0., b'zforce=', 0., b'energy=', 0., b'setid', b'=', 1)],
dtype=[('f0', 'S3'), ('f1', '<i4'), ('f2', 'S7'), ('f3', '<f8'), ('f4', 'S7'), ('f5', '<f8'), ('f6', 'S7'), ('f7', '<f8'), ('f8', 'S7'), ('f9', '<f8'), ('f10', 'S5'), ('f11', 'S1'), ('f12', '<i4')])

添加usecols

In [55]: dly_usecols = [1,7]
In [56]: data=np.genfromtxt(txt.splitlines(),dtype=None, comments=None,usecols=dly_usecols)
In [57]: data
Out[57]:
array([(39584, 0.), (39585, 0.)],
dtype=[('f0', '<i4'), ('f1', '<f8')])

与位置分隔符相同。

这样列分隔符就不会覆盖注释。在继续使用分隔符分割行之前,代码可能会删除注释。

关于python - genfromtxt 返回 -1 和 nan 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45272832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com