Parse lines into individual segments - python
Question: I'm new to python and having issues working with a text file. The text file
structure being used is shown. What I'm trying to do is first split the two
polylines into their own variable and then split each variable into individual
coordinates. The end goal is to have it structured as:

    
    
    polyline 1:
    [###, ###] [###, ###]
    
    polyline 2:
    [###, ###] [###, ###]
    

Text file structure:

    
    
    Polyline;
    
    1: ###,###; ###,###
    
    2: ###,###; ###,###; ###,###
    

The code I've tried is just working with a single line. While I've been able
to split the single line, I have not been able to move to the next step which
is to split the line further.

    
    
        f=open('txt.txt', 'r')
        pl = []
        for line in f.read().split('\n'):
            if (line.find('1: ') !=-1):
                ln = line.split('1: ')
                print ln
        f.close()
    

What is the best way to split the line to the end state?


Answer: First of all you can use `with ... as` statement to open a file which will
close the file at the end of block , secondly you don't have to read the file
and split with `\n` just use a for loop to loop over your file object.

Also for checking the start with digit number you can us regex and in this
case you can use `re.match` function, then you can split the line with `;` and
using a list comprehension split another parts with `,` :

    
    
    import re
    with open('txt.txt') as f:
        for line in f:
            if re.match(r'\d:.*',line):
                ln = [var.split(',') for var in line.split(';')]
                print ln
    

