Hi
any idea of how to parse a file like this. The problem that I found reading a line a putting into a list that it does not respect the blanks, I need to parse first 9 positions, then 8 positions then 8 positions, then 7, then 7 then 7 then 7

012345678901234567890123456789012345678901234567890

abcdefgh: 1234 5678 45 45 45 45
abcdefgh: 1234 45 45 45
abcdefgh: 1234 45 45 45
abcdefgh: 1234 5678 45 45 45
abcdefgh: 1234 5678 45 45 45

into

012345678901234567890123456789012345678901234567890

|abcdefgh:| 1234| 5678 | 45 | 45 | 45 | 45 |
|abcdefgh:| 1234| | 45 | | 45 | 45 |
|abcdefgh:| 1234| | 45 | 45 | | 45 |
|abcdefgh:| 1234| 5678 | | 45 | 45 | 45 |
|abcdefgh:| 1234| 5678 | 45 | 45 | 45 | |

Use code tags for fixed format text

012345678901234567890123456789012345678901234567890

 abcdefgh: 1234    5678     45      45     45     45
 abcdefgh: 1234             45             45     45
 abcdefgh: 1234             45      45            45
 abcdefgh: 1234    5678             45     45     45
 abcdefgh: 1234    5678     45      45     45      

into

 012345678901234567890123456789012345678901234567890

|abcdefgh:| 1234| 5678  | 45   | 45   | 45  | 45   |
|abcdefgh:| 1234|       | 45   |      | 45  | 45   |
|abcdefgh:| 1234|       | 45   | 45   |     | 45   |
|abcdefgh:| 1234| 5678  |      | 45   | 45  | 45   |
|abcdefgh:| 1234| 5678  | 45   | 45   | 45  |      |

Something like:

>>> data="""
 abcdefgh: 1234    5678     45      45     45     45
 abcdefgh: 1234             45             45     45
 abcdefgh: 1234             45      45            45
 abcdefgh: 1234    5678             45     45     45
 abcdefgh: 1234    5678     45      45     45      """.lstrip().splitlines()
>>> for row in data:
	row = row.lstrip().ljust(55)
	print('|'.join(('',row[:9], row[10:15], row[15:23], row[23:33], row[33:39], row[39:45], row[45:],'')))


|abcdefgh:|1234 |   5678 |    45    |  45  |   45 |    45    |
|abcdefgh:|1234 |        |    45    |      |   45 |    45    |
|abcdefgh:|1234 |        |    45    |  45  |      |    45    |
|abcdefgh:|1234 |   5678 |          |  45  |   45 |    45    |
|abcdefgh:|1234 |   5678 |    45    |  45  |   45 |          |
>>>

Also

def parse(line, pairs):
    a = 0
    for n, rep in pairs:
        for i in range(rep):
            yield line[a:a+n]
            a += n
        
pattern = [(9,1), (8,2), (7,4)]
line = "abcdefgh: 1234    5678     45      45     45     45"

print( tuple(parse(line, pattern)) )

""" my output -->
('abcdefgh:', ' 1234   ', ' 5678   ', '  45   ', '   45  ', '   45  ', '   45')
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.