[Numpy-discussion] Difference between np.loadtxt depending on whether you supply a file object or a filename

Sebastian Berg sebastian at sipsolutions.net
Mon Aug 20 07:14:08 EDT 2012


Hello,

On Mo, 2012-08-20 at 20:55 +1000, Andrew Nelson wrote:
> Dear list,
> I observe a difference when I try to load a 2D numpy array from a file
> object compared to if I supply a filename viz:
> 
> 
> >>> np.version.version
> '1.5.1'
> >>> f=open('fit_theoretical.txt')
> >>> a=np.loadtxt(f)
> >>> a.shape
> (1000,)
> >>> a=np.loadtxt('fit_theoretical.txt')
> >>> a.shape
> (500, 2)

There is actually a difference between the two, because np.loadtxt opens
the file with open(..., 'U'), which means that newlines might be
interpreted differently (because of difference between linux/windows/mac
newlines using \n, \r, etc. So the problem is that newlines are
interpreted wrong for you if you open with just the default mode.

Regards,

Sebastian
> 
> This strikes me as unexpected, it's not a documented behaviour. Any
> ideas?
> 
> 
> cheers,
> Andrew.
> 
> 
> 
> 
> -- 
> _____________________________________
> Dr. Andrew Nelson
> 
> 
> _____________________________________
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list