Python从文件中读取数据并转换为双精度
我一直在用python读取一个ASCII数据文件。然后我将数据转换成一个numpy数组。 然而,我注意到这些数字正在四舍五入 例如,我在文件中的原始值是:2368999.932089 哪条python四舍五入为:2368999.93209 下面是我的代码示例:Python从文件中读取数据并转换为双精度,python,numpy,Python,Numpy,我一直在用python读取一个ASCII数据文件。然后我将数据转换成一个numpy数组。 然而,我注意到这些数字正在四舍五入 例如,我在文件中的原始值是:2368999.932089 哪条python四舍五入为:2368999.93209 下面是我的代码示例: import numpy as np datafil = open("test.txt",'r') tempvar = [] header = datafil.readline() for line in datafil: wo
import numpy as np
datafil = open("test.txt",'r')
tempvar = []
header = datafil.readline()
for line in datafil:
word = line.split()
char = word[0] # take the first element word[0] of the list
word.pop() # remove the last element from the list "word"
if char[0:3] >= '224' and char[0:3] < '225':
tempvar.append(word)
strvar = np.array(tempvar,dtype = np.longdouble) # Here I want to read all data as double
print(strvar.shape)
var = strvar[:,0:23]
print(var[0,22]) # here it prints 2368999.93209 but the actual value is 2368999.932089
将numpy导入为np
datafil=open(“test.txt”,“r”)
tempvar=[]
header=datafil.readline()
对于datafil中的行:
word=line.split()
char=word[0]#取列表中的第一个元素单词[0]
word.pop()#从列表“word”中删除最后一个元素
如果char[0:3]>='224'和char[0:3]<'225':
tempvar.append(word)
strvar=np.array(tempvar,dtype=np.longdouble)#这里我想将所有数据都读取为double
打印(标准形状)
var=strvar[:,0:23]
print(var[0,22])#这里它打印2368999.93209,但实际值是2368999.932089
有什么想法吗,伙计们
Abedin我不完全确定您想做什么,但通过test.txt简化了它,它只包含
asdf
2368999.932089
然后是代码:
import numpy as np
datafil = open("test.txt",'r')
tempvar = []
header = datafil.readline()
for line in datafil:
tempvar.append(line)
print(tempvar)
strvar = np.array(tempvar, dtype=np.float)
print(strvar.shape)
print(strvar)
我得到以下输出:
$ python3 so.py
['2368999.932089']
(1,)
[ 2368999.932089]
看起来效果不错
编辑:使用提供的行进行更新,因此test.txt是
asdf
t JD a e incl lasc aper truean rdnnode RA Dec RArate Decrate metdr1 metddr1 metra1 metdec1 metbeta1 metdv1 metsl1 metarrJD1 beta JDej name 223.187263 2450520.619348 3.12966 0.61835 70.7196 282.97 171.324 -96.2738 1.19968 325.317 35.8075 0.662368 0.364967 0.215336 3.21729 -133.586 46.4884 59.7421 37.7195 282.821 2450681.900221 0 2368999.932089 EH2003
代码呢
import numpy as np
datafil = open("test.txt",'r')
tempvar = []
header = datafil.readline()
for line in datafil:
tempvar.append(line.split(' '))
print(tempvar)
strvar = np.array(tempvar[0][-2], dtype=np.float)
print(strvar)
最后一次打印
仍然为我输出2368999.932089
。所以我猜这是一个平台问题?如果强制执行dtype=np.float64
或dtype=np.float128
,会发生什么情况?其他一些合理性检查:在文本转换为浮点之前,您是否尝试过吐出文本?你从做这样的事情中得到了什么:
>>> np.array('2368999.932089')
array('2368999.932089',
dtype='<U14')
>>> float('2368999.932089')
2368999.932089
>>np.array('2368999.932089')
阵列('2368999.932089',
dtype='我认为这不是代码的问题。这是Python中常用的浮点表示法。请参阅
我想当你打印它时,print
已经将你的号码格式化为str
In [1]: a=2368999.932089
In [2]: print a
2368999.93209
In [3]: str(a)
Out[3]: '2368999.93209'
In [4]: repr(a)
Out[4]: '2368999.932089'
In [5]: a-2368999.93209
Out[5]: -9.997747838497162e-07
我无法重现您的问题。请提供正确的代码:您的注释不是python注释。很抱歉@Daniel。我刚刚更正了代码中的注释!您好@Alex Z!奇怪的是,我的操作与您的示例完全相同,但仍然生成236899.93209。不幸的是,我无法复制和粘贴我的数据文件,因为它包含7000万行和24行列。简单的一行测试文件仍然存在问题吗?再次您好!当我使用您提供的示例数据和代码时没有问题。它按预期打印数字。我真的不明白为什么它在我的原始代码中对其进行舍入???您能发布原始文件的一行或两行吗?有什么奇怪/不寻常的地方吗文件编码,还是纯文本?我可以在[9]:strvar=np.array([2368999.932089],dtype=np.longdouble)在[10]:print strvar[0]2368999.93209中重现这个问题