Search code examples
pythonsliceraw

Raw number data management in python


As we know python has a grammar system for recognize different types of data, specifically for number this is an integer: 87665 and this is a float: 667.03

My problem is that I have equipment raw data in the following format(example):

 22.5946120.0000  9.2703108.3784 30.2703 35.4054  4.8378 74.3243
 51.8919 96.4865145.6757 63.7838  6.1892 12.4054 58.1081145.4054
 61.3514 52.1622 16.0000 38.6486 38.1081 75.9459 59.1892  9.8649
 45.6757 38.3784 45.9459 47.0270 51.8919 36.4865  4.6757 42.4324
 58.6486 28.1081 32.9730 58.1081  8.7568 45.1351 59.4595 30.5405

as you can see they would be floats on python, every "number" takes 8 spaces including the dot, is like this: 3 spaces before the dot/dot/4 spaces after the dot, so for example I could have the number "0.01" but in the raw data it will appear as "--0.01--", or I could have "562.4001", originally this was supposed to be processed with Fortran, but I prefer python.

I tried to stablish a rule for processing the data, like:

in data replace "  " for ", "

being "data" a string of the raw number (I just converted the entire number's block into a string and the treated it like that) but sometimes there's just 1 space between 2 different number and sometimes there is no space at all. So basically I want to python to learn that in that string, every 8 spaces is a new float, I think that is possible with some slice command but I haven't got it quite yet... help...


Solution

  • Split the line into chunks of 8 characters. Strip the spaces from around each chunk, then call float() to convert them to floats.

    def line_to_floats(line, width):
        return list(map(float, [line[i:i+width].strip() for i in range(0, len(line), width)]))
    
    text = ''' 22.5946120.0000  9.2703108.3784 30.2703 35.4054  4.8378 74.3243
     51.8919 96.4865145.6757 63.7838  6.1892 12.4054 58.1081145.4054
     61.3514 52.1622 16.0000 38.6486 38.1081 75.9459 59.1892  9.8649
     45.6757 38.3784 45.9459 47.0270 51.8919 36.4865  4.6757 42.4324
     58.6486 28.1081 32.9730 58.1081  8.7568 45.1351 59.4595 30.5405'''
    for line in text.split('\n'):
        print(line_to_floats(line, 8))