[Numpy-discussion] Problem with loadtxt
Warren Weckesser
warren.weckesser@enthought....
Tue Aug 17 19:13:55 CDT 2010
Scott MacDonald wrote:
> Hi,
>
> I have a text file called 'blah' that contains only the following line:
>
> 5399354557888517312,5399354557888517312
>
> I want to load these into a numpy array as unit64's. The following
> script demonstrates my problem:
>
> import numpy as np
>
> with open('blah', 'r') as f:
> x = np.loadtxt(f, delimiter=',', dtype=(np.uint64, np.uint64))
>
> with open('blah', 'r') as f:
> for line in f:
> print line
>
> print x
>
>
> The output I get is:
>
> 5399354557888517312,5399354557888517312
>
> [5399354557888517120 5399354557888517120]
>
>
> Am I doing something wrong?
>
I don't know the full story, but it looks like the conversion from text
to an integer might go through a float:
In [27]: int(float(5399354557888517312))
Out[27]: 5399354557888517120L
You can change this by explicitly giving a converter to loadtxt:
In [31]: with open('blah', 'r') as f:
....: x = np.loadtxt(f, delimiter=',', dtype=(np.uint64,
np.uint64), converters={0:int})
....:
....:
In [32]: x
Out[32]: array([5399354557888517312, 5399354557888517120], dtype=uint64)
Note that the first column, for which I explicitly told it to use int()
as the converter, is correct.
Warren
> Thanks for your help,
> Scott
> ------------------------------------------------------------------------
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list