Convert JSON to CSV using Python (Idle)

前端 未结 4 1441
旧巷少年郎
旧巷少年郎 2021-02-14 06:31

I have a JSON file of Latitude/Longitude that I want to covert to a CSV file. I want to do this using Python. I have read/tried all other stackoverflow and google search results

4条回答
  •  一生所求
    2021-02-14 07:03

    This will help you iterate each item and write it to your csv file:

    import json, csv
    
    x = """[
        {"longitude":"-73.689070","latitide":"40.718000"},
        {"longitude":"-73.688400","latitide":"40.715990"},
        {"longitude":"-73.688340","latitide":"40.715790"},
        {"longitude":"-73.688370","latitide":"40.715500"},
        {"longitude":"-73.688490","latitide":"40.715030"},
        {"longitude":"-73.688810","latitide":"40.714370"},
        {"longitude":"-73.688980","latitide":"40.714080"},
        {"longitude":"-73.689350","latitide":"40.713390"},
        {"longitude":"-73.689530","latitide":"40.712800"},
        {"longitude":"-73.689740","latitide":"40.712050"},
        {"longitude":"-73.689820","latitide":"40.711810"},
        {"longitude":"-73.689930","latitide":"40.711380"},
        {"longitude":"-73.690110","latitide":"40.710710"}
    ]"""
    
    jsoned = json.loads(x)
    
    with open("test.csv", "wb+") as csv_file:
        csv_writer = csv.writer(csv_file)
        for i in jsoned:
            csv_writer.writerow([i[u'longitude'],
                                 i[u'latitide']])
    

    Note, you're misspelling latitude (latitide) in your original post.

提交回复
热议问题