gpt4 book ai didi

python - 无法使用巨大的 TXT 文件将字符串转换为 float

转载 作者:行者123 更新时间:2023-11-30 22:13:17 25 4
gpt4 key购买 nike

我有一个巨大的文本文件It is the second txt file labeled hhrr1996221.txt.zip

我试图通过计数与时间来分析数据,时间从 2 毫秒开始,然后给出 6 个数据集(计数)并重复。

虽然我确实想提高我的编码技能,但自去年以来我就没有使用过 python。我使用了以下代码:

           import numpy as np
hh=np.loadtxt('hhrr1996221.txt', delimiter=',')
time= hh[1:,0]

我只是想测试一下我是否得到一个数组,我最终也想得到计数,然后绘制一个图表

我收到的错误消息是

runfile('C:/Users/fahad/.spyder-py3/untitled0.py', wdir='C:/Users/fahad/.spyder-py3')
Traceback (most recent call last):

File "<ipython-input-44-d1860d9262f7>", line 1, in <module>
runfile('C:/Users/fahad/.spyder-py3/untitled0.py', wdir='C:/Users/fahad/.spyder-py3')

File "C:\Users\fahad\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\fahad\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/fahad/.spyder-py3/untitled0.py", line 9, in <module>
hh=np.loadtxt('hhrr1996221.txt', delimiter=',')

File "C:\Users\fahad\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1092, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Users\fahad\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1019, in read_data




items = [conv(val) for (conv, val) in zip(converters, vals)]

File "C:\Users\fahad\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1019, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]

File "C:\Users\fahad\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 738, in floatconv
return float(x)

ValueError: could not convert string to float: '2.0 264 264 244 252 504 252'

我将不胜感激所有我能提供的帮助,如果你们有我可以使用的不同代码,请通知我并指导我完成它。

干杯。

最佳答案

您的分隔符是空格。

尝试:

import numpy as np
hh=np.loadtxt(filename, delimiter=' ', skiprows=1)
time= hh[1:,0]
print( time )

输出:

[  2.10000000e+00   2.20000000e+00   2.30000000e+00 ...,   8.64027000e+04
8.64028000e+04 8.64029000e+04]

关于python - 无法使用巨大的 TXT 文件将字符串转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828846/

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