我想在python中读取如下文本文件
文本文件示例
{# "Column 4-5 Quantity": "Re/Im Pressure"
# "Column 4-5 Units": ""
# "Number Rows": 3584879
#}
4.0740740741e-002 -5.0370370370e-002 7.3333333333e-002 9.4443035889e+002 -6.0709484863e+002
4.1481481481e-002 -5.0370370370e-002 7.3333333333e-002 9.0292657471e+002 -6.2604998779e+002
4.2222222222e-002 -5.0370370370e-002 7.3333333333e-002 8.2029248047e+002 -6.4528021240e+002
4.2962962963e-002 -5.0370370370e-002 7.3333333333e-002 7.1829382324e+002 -6.5028546143e+002
4.3703703704e-002 -5.0370370370e-002 7.3333333333e-002 1.#QNAN00000e+000 0.0000000000e+000
4.4444444444e-002 -5.0370370370e-002 7.3333333333e-002 1.#QNAN00000e+000 0.0000000000e+000
4.5185185185e-002 -5.0370370370e-002 7.3333333333e-002 1.#QNAN00000e+000 0.0000000000e+000
4.5925925926e-002 -5.0370370370e-002 7.3333333333e-002 1.#QNAN00000e+000 0.0000000000e+000
-3.2592592593e-002 -4.9629629630e-002 7.3333333333e-002 4.9716027832e+002 -1.2688856201e+003
-3.1851851852e-002 -4.9629629630e-002 7.3333333333e-002 5.3677227783e+002 -1.4196939697e+003
-3.1111111111e-002 -4.9629629630e-002 7.3333333333e-002 6.1588317871e+002 -1.5679577637e+003
-3.0370370370e-002 -4.9629629630e-002 7.3333333333e-002 7.2673925781e+002 -1.7039268799e+003
-2.9629629630e-002 -4.9629629630e-002 7.3333333333e-002 8.5946520996e+002 -1.7935989990e+003
-2.8888888889e-002 -4.9629629630e-002 7.3333333333e-002 1.0030370483e+003 -1.7982950439e+003
因此我使用了numpy的函数loadtxt和genfromtxt
首先我尝试了loadtxt 代码是
impoty numpy as np
readtxt = np.loadtxt("filename.txt")
我收到如下错误消息
Wrong number of columns at line 5
其次我试过genfromtxt 代码是
impoty numpy as np
readtxt = np.genfromtxt("filename.txt")
我收到如下错误消息
Line #5 (got 5 columns instead of 4)
那么,我怎么能弄清楚这个问题呢? 我认为问题是" 1.#QNAN00000e + 000"
我想将此值更改为" 0"
答案 0 :(得分:1)
您可以使用comments
和missing_values
kwds。 comments
我们需要将其设置为默认值#
以外的其他内容,以便解析器不会在行中间停止读取。 missing values
我们设置为'1.#QNAN0000e+000'
,因此可以优雅地处理它们。这些设置为nan
但nan
之后您可以轻松替换为零result[np.isnan(result)] = 0
}:
>>> np.genfromtxt(<your_file>, comments='%', missing_values='1.#QNAN0000e+000')
array([[ 4.07407407e-02, -5.03703704e-02, 7.33333333e-02,
9.44430359e+02, -6.07094849e+02],
[ 4.14814815e-02, -5.03703704e-02, 7.33333333e-02,
9.02926575e+02, -6.26049988e+02],
[ 4.22222222e-02, -5.03703704e-02, 7.33333333e-02,
8.20292480e+02, -6.45280212e+02],
[ 4.29629630e-02, -5.03703704e-02, 7.33333333e-02,
7.18293823e+02, -6.50285461e+02],
[ 4.37037037e-02, -5.03703704e-02, 7.33333333e-02,
nan, 0.00000000e+00],
[ 4.44444444e-02, -5.03703704e-02, 7.33333333e-02,
nan, 0.00000000e+00],
[ 4.51851852e-02, -5.03703704e-02, 7.33333333e-02,
nan, 0.00000000e+00],
[ 4.59259259e-02, -5.03703704e-02, 7.33333333e-02,
nan, 0.00000000e+00],
[-3.25925926e-02, -4.96296296e-02, 7.33333333e-02,
4.97160278e+02, -1.26888562e+03],
[-3.18518519e-02, -4.96296296e-02, 7.33333333e-02,
5.36772278e+02, -1.41969397e+03],
[-3.11111111e-02, -4.96296296e-02, 7.33333333e-02,
6.15883179e+02, -1.56795776e+03],
[-3.03703704e-02, -4.96296296e-02, 7.33333333e-02,
7.26739258e+02, -1.70392688e+03],
[-2.96296296e-02, -4.96296296e-02, 7.33333333e-02,
8.59465210e+02, -1.79359900e+03],
[-2.88888889e-02, -4.96296296e-02, 7.33333333e-02,
1.00303705e+03, -1.79829504e+03]])