Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing data with numpy.loadtxt: No delimiters but fixed column width

Tags:

python

numpy

I'm trying to import data with python/numpy.loadtxt. With most of the data this isn't a problem, e.g. if a row looks like this:

0.000000      0.000000      0.000000      0.000000    -0.1725804E-13

In this case I can use white space as the delimiter. Unfortunately the program which produces the data doesn't use delimiters, just a fixed column width (and I can't change that). Example:

-0.1240503E-03-0.6231297E-04  0.000000      0.000000    -0.1126164E-02

Can I tell numpy.loadtxt in some easy way that every column is 14 characters? I'd prefer to not have to modify the files the other program produces manually...

EDIT:

I thought I share my very simple solution based on dxwx's suggestion. For the example I provided the solution would be

a = numpy.genfromtxt('/path/to/file.txt', delimiter = 14)

There was a additional whitespace before the first column in my real data, and I didn't want to use the last column and the last row. So it looks like this now:

a = numpy.genfromtxt('/path/to/file.txt',
                     delimiter = (1,14,14,14,14,14,14), 
                     usecols = range(1,6), skip_footer = 1)

Thanks everyone for the fast response.

like image 801
oli Avatar asked Jul 04 '13 10:07

oli


1 Answers

Have a look at Numpy's genfromtxt - that says it can use an integer width for the separator.

like image 119
dwxw Avatar answered Oct 11 '22 22:10

dwxw