我一直在使用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
任何想法的家伙?
阿贝丁
我无法重现你的问题。请提供正确的代码:您的评论不是python评论。 – Daniel
对不起@Daniel。我刚纠正了我的代码中的评论! – user3578925