Delimiters for python open

Viewed 42

I was wondering if there was a way to specify a regex delimiter while using python's open and reading my file line by line?

The problem I'm coming across is that certain lines are within brackets and contain commas so they're being read as an extra column. I have a regex that would keep them as the same column.

Here's an example of the code:

with open('TEST.txt') as text_file:
    lines = [line.strip() for line in text_file.readlines()]
    for index, line in enumerate(lines):
        #print(line)
        #do more code stuff

and a working regex:

,(?![^\[\(]*[$\]\)])

Here's an example of the data that causes problems:

11/02/20 22:00:00,0:01,6:00,1234,1234,3456,8765,Good,[Data:Data],,,,,Fruits,Very(Good),[123:456:789,234567:890]

You can see in the very last column there's a comma within brackets that gets split into another column.

The correct readout would not separate the last column in brackets :

Thanks in advance!

0 Answers
Related