Import data using numpy.loadtxt: without delimiters but with a fixed column width

I am trying to import data using python / numpy.loadtxt. With most data, this is not a problem, for example. if the line looks like this:

0.000000 0.000000 0.000000 0.000000 -0.1725804E-13 

In this case, I can use the space as a separator. Unfortunately, the program that produces the data does not use separators, but only a fixed column width (and I cannot change this). Example:

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

Is it possible to indicate to numpy.loadtxt several times that each column has 14 characters? I would prefer not to modify files created by another program manually ...

EDIT:

It seemed to me that I share a very simple solution based on the dxwx suggestion. In this example, I decided that the solution would be

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

An extra space appeared in front of the first column in my real data, and I did not want to use the last column and the last row. Now it looks like this:

 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 quick reply.

+4
source share
2 answers

Look at the Numpy genfromtxt - it says it can use integer width for the delimiter.

+3
source

I would use numpy.fromregex . Then you can simply define a basic regex to capture up to 14 characters.

So, here we fix each field with the RE [-.\dE]{1,14} group (which assumes the absence of missing values ​​and that the format always matches the example you gave, regarding which characters are possible):

 >>> regex = r"([-.\dE]{1,14})\s*([-.\dE]{1,14})\s*([-.\dE]{1,14})\s*([-.\dE]{1,14})\s*([-.\dE]{1,14})" >>> np.fromregex(dat, regex, [('A', np.float32), ('B', np.float32),('C', np.float32),('D', np.float32),('E', np.float32),]) array([ (-0.0001240503042936325, -6.231296720216051e-05, 0.0, 0.0, -0.0011261639883741736) ], dtype=[('A', '<f4'), ('B', '<f4'), ('C', '<f4'), ('D', '<f4'), ('E', '<f4')]) 
+1
source

Source: https://habr.com/ru/post/1489665/


All Articles