[Numpy-discussion] numpy loadtxt - ValueError: setting an array element with a sequence.

Christopher Barker Chris.Barker at noaa.gov
Thu Oct 29 12:29:20 EDT 2009


Peter Schmidtke wrote:
> Have you tried the numpy.fromfile function?

good point -- fromfile() can be much faster for the simple cases it can 
handle.

 > not all lines ave the same lenght (not the
> same number of values).
> 
> What would be the best way to load this?

That depends on what the data mean. Is it a 2-d array with missing 
values? If so, how do you know which are missing? Are there the same 
number of tabs in each row? If do than loadtxt should be able to handle it.

You may be best off looping through the file:

for line in file:
   a = numpy.fromstring(line, sep='\t', dtype=np.float)

and do what makes sense with each line.

-Chris









-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list