gpt4 book ai didi

python - numpy读取txt错误(loadtxt,genfromtxt)

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:45 25 4
gpt4 key购买 nike

我想在 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”

最佳答案

您可以使用commentsmissing_values kwd。 comments 我们需要设置为默认的 # 以外的内容,这样解析器就不会在行的中间停止读取。我们将缺失值设置为'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]])

关于python - numpy读取txt错误(loadtxt,genfromtxt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065459/

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