printing a list that contained on Json file with python

3677 views python
3

I am new to python and i am working with some geojson files that contain a multiple objects each object represents a precinct. I need to print the coordinates for all of the precints how can i do this? i was trying this but it does not work:

import json
with open('districts and precinc data merged.json') as f:
    data = json.load(f)
for i in json['features']:
  print(i['geometry']['coordinates'])

this is an example of the json file:

    {"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[552346.2856999999,380222.8998000007]]]]},"properties":{"OBJECTID":1,"STFID":"55001442500001","NAME":"0001"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]]]},"properties":{"OBJECTID":2,"STFID":"55001537250001","NAME":"0001","COUSUBFP":"53725"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]]]},"properties":{"OBJECTID":6278,"STFID":"55141885750001","NAME":"0001","COUSUBFP":"88575"}}
]}

my desired output would be a for each line have the cordinates for each object like so:

[552346.2856999999,380222.8998000007]
[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]
[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]

thank you for your help!

answered question

li deisred output would be something like a line for each object containing the districts. for example [552346.2856999999,380222.8998000007] in one line and [529754.7249999996,409135.9135999996],[529740.0305000003,408‌​420.03810000047] on the next one and so on

It's better to edit the question to include that instead of a comment down here. You can format it better too.

sorry about that i have now edited the question :)

1 Answer

11

you can unpack the list, or simply loop through it,

x =  {"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[552346.2856999999,380222.8998000007]]]},"properties":{"OBJECTID":1,"STFID":"55001442500001","NAME":"0001"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[529754.7249999996,409135.9135999996],[529740.0305000003,408420.03810000047]]]},"properties":{"OBJECTID":2,"STFID":"55001537250001","NAME":"0001","COUSUBFP":"53725"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[508795.9363000002,441655.3672000002],[508813.49899999984,441181.034]]]},"properties":{"OBJECTID":6278,"STFID":"55141885750001","NAME":"0001","COUSUBFP":"88575"}}
]}

for i in x["features"]:
    print(*i["geometry"]["coordinates"][0])

posted this

Have an answer?

JD

Please login first before posting an answer.