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.
Have a look at Numpy's genfromtxt - that says it can use an integer width for the separator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With