[Numpy-discussion] Problem with loadtxt

Scott MacDonald scott.p.macdonald at gmail.com
Tue Aug 17 22:15:41 EDT 2010


Thanks Warren, that does solve the problem.  I am still confused however by
this:

In [42]: c = StringIO("5399354557888517120")

In [43]: np.loadtxt(c, dtype=np.uint64)
Out[43]: array(5399354557888517120L, dtype=uint64)

Does the above seem contradictory to you?

Thanks,
Scott

On Tue, Aug 17, 2010 at 5:13 PM, Warren Weckesser <
warren.weckesser at enthought.com> wrote:

> 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 at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100817/6021964f/attachment.html>


More information about the NumPy-Discussion mailing list