Zip Csv String Containing Geojson To Python Dictionary
I have a CSV string with a GeoJSON string in it. str='''LC08,2016-08-02,'{'type':'Polygon','coordinates':[[[10,20],[50,40],[60,80],[15,45 ],[10,20]]]}',-9999,-9999''' I intend to
Solution 1:
What you (probably) want is splitting with regular expressions, more precisely with the regex
module:
import regex as re
string = """LC08,2016-08-02,"{'type':'Polygon','coordinates':[[[10,20],[50,40],[60,80],[15,45 ],[10,20]]]}",-9999,-9999"""
rx = re.compile(r"""\{[^{}]+\}(*SKIP)(*FAIL)|,""")
d = {}
d['name'], d['date'], d['geometry'], d['value0'], d['value1'] = rx.split(string)
print(d)
Which yields
{'name': 'LC08', 'date': '2016-08-02', 'geometry': '"{\'type\':\'Polygon\',\'coordinates\':[[[10,20],[50,40],[60,80],[15,45 ],[10,20]]]}"', 'value0': '-9999', 'value1': '-9999'}
See a demo on regex101.com for the expression.
Post a Comment for "Zip Csv String Containing Geojson To Python Dictionary"