printing a list that contained on Json file with python












0















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!










share|improve this 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,408420.03810000047] on the next one and so on

    – tony
    Nov 26 '18 at 1:51











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

    – gilch
    Nov 26 '18 at 1:51













  • sorry about that i have now edited the question :)

    – tony
    Nov 26 '18 at 1:56
















0















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!










share|improve this 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,408420.03810000047] on the next one and so on

    – tony
    Nov 26 '18 at 1:51











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

    – gilch
    Nov 26 '18 at 1:51













  • sorry about that i have now edited the question :)

    – tony
    Nov 26 '18 at 1:56














0












0








0








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!










share|improve this question
















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!







python json geojson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 1:55







tony

















asked Nov 26 '18 at 1:45









tonytony

14




14













  • 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,408420.03810000047] on the next one and so on

    – tony
    Nov 26 '18 at 1:51











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

    – gilch
    Nov 26 '18 at 1:51













  • sorry about that i have now edited the question :)

    – tony
    Nov 26 '18 at 1:56



















  • 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,408420.03810000047] on the next one and so on

    – tony
    Nov 26 '18 at 1:51











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

    – gilch
    Nov 26 '18 at 1:51













  • sorry about that i have now edited the question :)

    – tony
    Nov 26 '18 at 1:56

















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,408420.03810000047] on the next one and so on

– tony
Nov 26 '18 at 1:51





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,408420.03810000047] on the next one and so on

– tony
Nov 26 '18 at 1:51













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

– gilch
Nov 26 '18 at 1:51







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

– gilch
Nov 26 '18 at 1:51















sorry about that i have now edited the question :)

– tony
Nov 26 '18 at 1:56





sorry about that i have now edited the question :)

– tony
Nov 26 '18 at 1:56












1 Answer
1






active

oldest

votes


















0














You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].



Then you can use nested loop,



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"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `n` with `,`
print("b") # removes trailing ,

# output,

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





share|improve this answer


























  • it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

    – tony
    Nov 26 '18 at 2:02











  • you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

    – Sufiyan Ghori
    Nov 26 '18 at 2:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473787%2fprinting-a-list-that-contained-on-json-file-with-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].



Then you can use nested loop,



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"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `n` with `,`
print("b") # removes trailing ,

# output,

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





share|improve this answer


























  • it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

    – tony
    Nov 26 '18 at 2:02











  • you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

    – Sufiyan Ghori
    Nov 26 '18 at 2:06
















0














You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].



Then you can use nested loop,



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"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `n` with `,`
print("b") # removes trailing ,

# output,

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





share|improve this answer


























  • it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

    – tony
    Nov 26 '18 at 2:02











  • you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

    – Sufiyan Ghori
    Nov 26 '18 at 2:06














0












0








0







You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].



Then you can use nested loop,



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"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `n` with `,`
print("b") # removes trailing ,

# output,

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





share|improve this answer















You have an error in your json. In first line you have 3 list open [[[ but closing 4 ]]]]. In your json file, replace [[[552346.2856999999,380222.8998000007]]]] with [[[552346.2856999999,380222.8998000007]]].



Then you can use nested loop,



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"]:
for j in i["geometry"]["coordinates"][0]:
print(j, end=",") # replace `n` with `,`
print("b") # removes trailing ,

# output,

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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 2:12

























answered Nov 26 '18 at 1:59









Sufiyan GhoriSufiyan Ghori

11.7k95783




11.7k95783













  • it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

    – tony
    Nov 26 '18 at 2:02











  • you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

    – Sufiyan Ghori
    Nov 26 '18 at 2:06



















  • it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

    – tony
    Nov 26 '18 at 2:02











  • you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

    – Sufiyan Ghori
    Nov 26 '18 at 2:06

















it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

– tony
Nov 26 '18 at 2:02





it gives me the following error: for i in json['features']: TypeError: 'module' object is not subscriptable

– tony
Nov 26 '18 at 2:02













you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

– Sufiyan Ghori
Nov 26 '18 at 2:06





you have an error in your file, there is extra ]. Please copy the json frm my code above and replace it in your file first

– Sufiyan Ghori
Nov 26 '18 at 2:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473787%2fprinting-a-list-that-contained-on-json-file-with-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen