[SciPy-User] Loading a CSV with loadtxt

Esko Lehtonen esko.lehtonen@iki...
Fri Oct 29 06:28:12 CDT 2010

27.10.2010 22:19, Athanasios Anastasiou kirjoitti:
> Hello everyone
> I have a CSV file with the following format:
> Unicode<100, Unicode<100, float, float, uint
> and i am trying to read it into a numpy array using:
> theData = numpy.loadtxt('primData.csv',delimiter=',',skiprows=1,dtype={'names':('NodeS','NodeT','flowF','flowB','ID'),'formats':('U100','U100','f','f','uint')})
> This fails with "expected a readable buffer object". I have also tried
> first opening the file through codecs as a utf-8 file and passing the
> file object into loadtxt but i still get the same error. And i have
> also tried defining the dtype outside of loadtxt and making sure that
> it works before using it any further.
> However, when i tried the code that is available at the cookbook:
> (with some minor modifications)
> http://www.scipy.org/Cookbook/InputOutput#head-b0366eac0be19c3d7c32fc81c47a7c02508b6f52
> It works exactly as expected.
> I don't mind using the latter but i am just wondering what it is that
> i am doing wrong with loadtxt(?)
> Any ideas why loadtxt fails in this way?


I have encountered that error many times. Every time there was something 
wrong with the text file (e.g. a missing or extra comma somewhere, 
especially in the end of line, or maybe an empty line). loadtxt and 
genfromtxt are very picky with the format of files and the error message 
is not very helpful.

- Esko

More information about the SciPy-User mailing list