Python json.dumps。尝试转储我呼叫的部分并使其读取

时间:2019-04-05 04:01:48

标签: python json python-2.7

我有一个json,此文件继续运行,直到获得更高的id值为止。请注意最后的“ id”。在此示例中,我有id = 1和id = 2。我想保留它们并让json.dumps工作。

{  
   "type":"FeatureCollection",
   "features":[  
      {  
     "geometry":{  
        "type":"MultiLineString",
        "coordinates":[  
           [  
              [  
                 -74.00315442793773,
                 40.72766404544987
              ],
              [  
                 -74.0028172664358,
                 40.728363128134724
              ]
           ],
           [  
              [  
                 -74.0021489154904,
                 40.72803536727916
              ],
              [  
                 -74.00269315136833,
                 40.72831405338023
              ],
              [  
                 -74.0028172664358,
                 40.728363128134724
              ]
           ],
           [  
              [  
                 -74.0028172664358,
                 40.728363128134724
              ],
              [  
                 -74.00531369282497,
                 40.72860449338741
              ]
           ],
           [  
              [  
                 -74.0028172664358,
                 40.728363128134724
              ],
              [  
                 -74.00218010455211,
                 40.729716581196314
              ]
           ]
        ]
     },
     "type":"Feature",
     "properties":{  
        "Category":"Intersection Safety Improvements",
        "Shape_Length":1713.1547151760014,
        "MID_Y":204684.89628821754,
        "OBJECTID_1":1,
        "Name":"6th and Houston",
        "OBJECTID":1,
        "Year":2015.0,
        "MID_X":983107.3932405015,
        "Presentation_Location":"NA",
        "END_X":983645.7682569176,
        "START_X":983375.7005555928,
        "START_Y":204384.20742309093,
        "Shape_Leng":1713.15463977,
        "END_Y":205132.0018592626,
        "Borough":"M",
        "Presentation":"2015_m_6thavehouston_gallery.pdf"
     },
     "id":1
  },
  {  
     "geometry":{  
        "type":"LineString",
        "coordinates":[  
           [  
              -73.99677175771464,
              40.725431942931955
           ],
           [  
              -73.99584577607527,
              40.72520835286401
           ],
           [  
              -73.99532907980338,
              40.72508373862415
           ]
        ]
     },
     "type":"Feature",
     "properties":{  
        "Category":"Intersection Safety Improvements",
        "Shape_Length":419.51660817139646,
        "MID_Y":203507.53694658508,
        "OBJECTID_1":2,
        "Name":"Houston and Crosby",
        "OBJECTID":2,
        "Year":2015.0,
        "MID_X":985344.7207623449,
        "Presentation_Location":"NA",
        "END_X":985544.669314757,
        "START_X":985144.7882247567,
        "START_Y":203570.98398391902,
        "Shape_Leng":419.516589712,
        "END_Y":203444.1404055953,
        "Borough":"M",
        "Presentation":"2015_m_houstoncrosby_gallery.pdf"
     },
     "id":2
  },

我可以从ID中提取。但是,当我尝试打印我的json.dumps时,我“无”。我要转储“ id”。这里的代码出了什么问题?

import json

def main():
input = raw_input('JSON Input file path')
p = 0
with open(input, 'r') as jf:
    data = json.loads(jf.read())
    for item in data["features"]:
        p += 1
        item["id"] = p
        item
with open(input, 'w') as jf:
    json.dumps(data, jf)

0 个答案:

没有答案