Python convert WKT polygon to row wise points
Question: 
    "POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
    "POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
    

How can I get something like below in a csv file:

UID(like 101,102 etc) represents an unique identifier for each polygon.

    
    
    UID#1,County,population,Point#1_Lat,Point#1_Long
    UID#1,County,population,Point#2_Lat,Point#2_Long
    UID#1,County,population,Point#3_Lat,Point#3_Long
    UID#1,County,population,Point#n_Lat,Point#n_Long
    
    UID#2,County,population,Point#1_Lat,Point#1_Long
    UID#2,County,population,Point#2_Lat,Point#2_Long
    UID#2,County,population,Point#3_Lat,Point#3_Long
    UID#2,County,population,Point#n_Lat,Point#n_Long
    


Answer: Here is a solution using [`pyparsing`](http://pyparsing.wikispaces.com/). Let
me know if that doesn't work for you - it shouldn't be too difficult to come
up with something that only uses the standard library (e.g. `re`, etc.), but
it will definitely be uglier.

    
    
    import csv
    from pyparsing import Group, Literal, OneOrMore, Optional, Word
    from pyparsing import delimitedList
    from pyparsing import alphas, nums
    
    data = """
    "POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
    "POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
    """
    
    def parse_line(line):
        latitude = Word(nums)
        longitude = Word(nums)
        point = Group(latitude + longitude)
        point_sequence = delimitedList(point, delim=',')
    
        name = Word("POLYGON").suppress()
        paren_left = Literal("((").suppress()
        paren_right = Literal("))").suppress()
        quote = Literal('"').suppress()
        polygon = Group(quote + name + paren_left + point_sequence + paren_right + quote)
    
        uid = Word(nums)
        county = Word(alphas)
        population = Word(nums)
        sep = Literal(",").suppress()
        parser = polygon + sep + uid + sep + county + sep + population
    
        result = parser.parseString(line)
        return result
    
    def parse_lines(data, outfile):
        with open(outfile, 'w') as f:
            writer = csv.writer(f, lineterminator='\n')
            lines = data.split('\n')
            for line in lines:
                if not line:
                    continue
                points, uid, county, population = parse_line(line)
                for lat, long in points:
                    writer.writerow([uid, county, population, lat, long])
                writer.writerow('')
    
    parse_lines(data, r'd:\out.txt') # change the path to wherever you want output
    

Result:

    
    
    101,Something,100000,12,13
    101,Something,100000,22,23
    101,Something,100000,16,17
    101,Something,100000,22,24
    
    102,another,200000,10,12
    102,another,200000,40,42
    102,another,200000,46,34
    102,another,200000,16,24
    102,another,200000,88,22
    102,another,200000,33,24
    102,another,200000,18,20
    

