Python json.dumps。想把我打电话的那部分倒出来让人看

2024-04-28 09:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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时,我得到“none”。我想甩掉“身份证”。这里的代码出了什么问题

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)

Tags: idjsoninputdatatypepresentationitemstart