如何以指数格式从CSV读取值?
我有这种格式的值,其中e+002等是指数:
1.25663709640503E+0000 2.33967334032059E-0001
2.51327419281006E+0000 4.85565841197968E-0001
3.76991128921509E+0000 3.30846726894379E-0001
5.02654838562012E+0000 5.50593174993992E-0002
6.28318548202515E+0000 3.12543800100684E-0003
7.53982257843018E+0000 4.11923155188560E-0002
8.79645919799805E+0000 1.35717853903770E-0001
1.00530967712402E+0001 1.26785650849342E-0001
1.13097343444824E+0001 2.28818021714687E-0002
1.25663709640502E+0001 3.12676839530468E-0003
1.38230075836181E+0001 2.36203446984291E-0002
1.50796451568603E+0001 7.74327516555786E-0002
1.63362827301025E+0001 7.97238126397133E-0002
1.75929183959961E+0001 1.36453993618488E-0002
1.88495559692383E+0001 3.12899192795157E-0003
我试图使用此代码将它们读成一个numpy阵列:
import numpy as np
with open(r"fft_in.XY") as file_name:
array = np.loadtxt(file_name, delimiter=",")
print(array)
但是,它不起作用,我会遇到以下错误:
Traceback (most recent call last):
File "D:\Python_projects\rt_fft\main.py", line 4, in <module>
array = np.loadtxt(file_name, delimiter=",")
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: ' 0.00000000000000E+0000 0.00000000000000E+0000'
是否有人知道,是否知道,是否知道,是否知道吗有一个内置函数可以从CSV读取此格式到Numpy数组的数字?非常感谢您的任何答复和想法!
I have values in this format, where e+002 and so on is the exponent:
1.25663709640503E+0000 2.33967334032059E-0001
2.51327419281006E+0000 4.85565841197968E-0001
3.76991128921509E+0000 3.30846726894379E-0001
5.02654838562012E+0000 5.50593174993992E-0002
6.28318548202515E+0000 3.12543800100684E-0003
7.53982257843018E+0000 4.11923155188560E-0002
8.79645919799805E+0000 1.35717853903770E-0001
1.00530967712402E+0001 1.26785650849342E-0001
1.13097343444824E+0001 2.28818021714687E-0002
1.25663709640502E+0001 3.12676839530468E-0003
1.38230075836181E+0001 2.36203446984291E-0002
1.50796451568603E+0001 7.74327516555786E-0002
1.63362827301025E+0001 7.97238126397133E-0002
1.75929183959961E+0001 1.36453993618488E-0002
1.88495559692383E+0001 3.12899192795157E-0003
I tried to read them into a numpy array using this code:
import numpy as np
with open(r"fft_in.XY") as file_name:
array = np.loadtxt(file_name, delimiter=",")
print(array)
However, it does not work, I get the following errors:
Traceback (most recent call last):
File "D:\Python_projects\rt_fft\main.py", line 4, in <module>
array = np.loadtxt(file_name, delimiter=",")
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\achim\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\lib\npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: ' 0.00000000000000E+0000 0.00000000000000E+0000'
Does someone maybe know, whether there is a built-in function to read numbers in this format from csv into a numpy array? Thanks a lot for any replies and ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是您的代码,实际上可以正常工作。
问题是您的数据。
请注意,在此行中如何:
您有
delemiter =“,”
,但是您的数据中没有任何,
?如果将数据替换为:
,则您的脚本可以正常工作,因为它使用了Numpy期望的定界符。
为了使其与您拥有的数据一起使用,您应该更改:
to:
尽管您必须确保定界符始终包含相同数量的白色空间
The issue is not your code, that actually works fine.
The issue is your data.
Notice how in this line:
You have
delimiter=","
but you do not have any,
in your data?If you replace your data with:
Then your script works fine because it uses the delimiter that numpy expects.
To make it work with the data you have, you should change:
To:
Although you have to be sure that the delimiter always contains the same number of white spaces