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

Peter Schmidtke pschmidtke at mmb.pcb.ub.es
Thu Oct 29 09:19:50 EDT 2009


On Thu, 29 Oct 2009 05:30:09 -0700 (PDT), TheLonelyStar
<nabble2 at lonely-star.org> wrote:
> Adter trying the same thing in matlab, I realized that my "tsv" file is
not
> matrix-style. But this I mean, not all lines ave the same lenght (not the
> same number of values).
> 
> What would be the best way to load this?
> 
> Regards,
> Nathan

Use the numpy fromfile function :

For instance I read the file :

5       8       5       5.5     6.1
3       5.5
2       6.5


with : 
x=npy.fromfile("test.txt",sep="\t")

and it returns an array x :

array([ 5. ,  8. ,  5. ,  5.5,  6.1,  3. ,  5.5,  2. ,  6.5])

You can reshape this array to a 3x3 matrix using the reshape function -> 

x.reshape((3,3))

-- 

Peter Schmidtke

----------------------
PhD Student at the Molecular Modeling and Bioinformatics Group
Dep. Physical Chemistry
Faculty of Pharmacy
University of Barcelona




More information about the NumPy-Discussion mailing list