2014-05-24 53 views
0

我一直在使用python读取ASCII数据文件。然后我将数据转换成一个numpy数组。 但是,我注意到数字正在四舍五入。Python从文件读取数据并转换为双精度

E.g.我从文件原始值是:2368999.932089

其中Python已经四舍五入到:2368999.93209

这里是我的代码示例:

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 

任何想法的家伙?

阿贝丁

+0

我无法重现你的问题。请提供正确的代码:您的评论不是python评论。 – Daniel

+0

对不起@Daniel。我刚纠正了我的代码中的评论! – user3578925

回答

0

我不能完全确定你想要做什么,但只有包含

asdf 
2368999.932089 

的test.txt,然后将代码简化为:

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) 

最后print仍然输出2368999.932089我。所以我猜这是一个平台问题?如果您强制dtype=np.float64dtype=np.float128会发生什么情况?一些其他的理智检查:你有没有尝试在文本转换为浮动之前吐出文本?你从做什么就得到了什么:

>>> np.array('2368999.932089') 
array('2368999.932089', 
    dtype='<U14') 
>>> float('2368999.932089') 
2368999.932089 
+0

Hi @Alex Z!奇怪的是,我完全按照你的例子做了,但它仍然产生236899.93209。不幸的是,我无法复制和粘贴我的数据文件,因为它包含7000万行和24列。 – user3578925

+0

简单的单行测试文件仍然存在问题吗? –

+0

再次嗨!当我使用您提供的样本数据和代码时,没有任何问题。它按照它应该打印的数字打印。 我真的不明白为什么它将我的原始代码四舍五入? – user3578925

1

我认为这不是你的代码的问题。这是Python中通常的浮点表示法。见 https://docs.python.org/2/tutorial/floatingpoint.html

我认为当你打印出来,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