开发者

How to parse a file , find out the dict in it , and then create a dict out of it?

# This is an auto-generated Django model module created by ogrinspect.
from django.contrib.gis.db impor开发者_运维百科t models

class Parking(models.Model):
    name = models.CharField(max_length=80)
    descriptio = models.CharField(max_length=80)
    geom = models.PointField(srid=4326)
    objects = models.GeoManager()

# Auto-generated `LayerMapping` dictionary for Parking model
parking_mapping = {
    'name' : 'Name',
    'descriptio' : 'Descriptio',
    'geom' : 'POINT25D',
}

The parking_mapping is the dict in the whole string here . This whole output is stored in a text field . How to parse the text file , extract parking_mapping as a dict named parking_mapping . ?


use json module

mydict = {'name' : 'foo'}


with open('dump.json', 'w') as f:
    json_str = json.dumps(mydict) // save json_str in database 
    f.write(json_str)

contents = None
with open('dump.json') as f:
    json_str = json.loads(f.read())
    contents = json_str

You could walk over contents and look up dictionaries.


If you have control over the file why not just name it .py, and it to path, and import parking_mapping?

 from <file> import parking_mapping
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜