for a little (!) project i need to have the data (in DB ) and the maps-
this request delivers the Map
the issue:
we can fetch the data and finally export in xml or Geo Json
from xml to CSV
from GeoJson CSV
If we take a JSON file like the following we can go and convert to a CSV file - also with Python
tried like so:
JSON has nested objects, so it normally cannot be directly converted to CSV.
we gotta have to change that to something like this:
a. install the pandas package.
running the following command to install the pandas package under Windows:
With the pandas library, this is as easy - we can go and use two commands
Then, assuming the results were stored as df:
Based on the verbosity of previous answers, we should all thank pandas for the shortcut.
what do you think!?
this request delivers the Map
[timeout:600]; area["ISO3166-1"="BR"]->.brazil; area["ISO3166-1"="AR"]->.argentina; area["ISO3166-1"="VE"]->.venezuela; area["ISO3166-1"="PE"]->.peru; area["ISO3166-1"="CL"]->.chile; /* more areas..*/ ( nwr[amenity=hospital](area.brazil); nwr[amenity=hospital](area.argentina); nwr[amenity=hospital](area.venezuela); nwr[amenity=hospital](area.peru); nwr[amenity=hospital](area.chile); /* other queries... */ ); out center;... and this request delivers the data;
[out:csv(::id,::type,::lon, ::lat, "name","addr:postcode","addr:city","addr:street","addr:housenumber","contact:website"," contact:email=*")][timeout:600]; area["ISO3166-1"="BR"]->.brazil; area["ISO3166-1"="AR"]->.argentina; area["ISO3166-1"="VE"]->.venezuela; area["ISO3166-1"="PE"]->.peru; area["ISO3166-1"="CL"]->.chile; /* more areas..*/ ( nwr[amenity=hospital](area.brazil); nwr[amenity=hospital](area.argentina); nwr[amenity=hospital](area.venezuela); nwr[amenity=hospital](area.peru); nwr[amenity=hospital](area.chile); /* other queries... */ ); out center;i want to have both - the map and the data. But how to achive this. which request gives back both ?
the issue:
we can fetch the data and finally export in xml or Geo Json
from xml to CSV
from GeoJson CSV
If we take a JSON file like the following we can go and convert to a CSV file - also with Python
tried like so:
import json import csv f = open('data.json') data = json.load(f) f.close() f = open('data.csv') csv_file = csv.writer(f) for item in data: csv_file.writerow(item) f.close() However, it did not work. I am using Django and the error I received is: `file' object has no attribute 'writerow'` I then tried the following: import json import csv f = open('data.json') data = json.load(f) f.close() f = open('data.csv') csv_file = csv.writer(f) for item in data: f.writerow(item) # ← changed f.close() I then get the error: `sequence expected` Sample json file: [{ "pk": 22, "model": "auth.permission", "fields": { "codename": "add_logentry", "name": "Can add log entry", "content_type": 8 } }, { "pk": 23, "model": "auth.permission", "fields": { "codename": "change_logentry", "name": "Can change log entry", "content_type": 8 } }, { "pk": 24, "model": "auth.permission", "fields": { "codename": "delete_logentry", "name": "Can delete log entry", "content_type": 8 } }, { "pk": 4, "model": "auth.permission", "fields": { "codename": "add_group", "name": "Can add group", "content_type": 2 } }, { "pk": 10, "model": "auth.permission",But we can do this like so:
JSON has nested objects, so it normally cannot be directly converted to CSV.
we gotta have to change that to something like this:
{ "pk": 22, "model": "auth.permission", "codename": "add_logentry", "content_type": 8, "name": "Can add log entry" }, ......] Here is my code to generate CSV from that: import csv import json x = """[ { "pk": 22, "model": "auth.permission", "fields": { "codename": "add_logentry", "name": "Can add log entry", "content_type": 8 } }, { "pk": 23, "model": "auth.permission", "fields": { "codename": "change_logentry", "name": "Can change log entry", "content_type": 8 } }, { "pk": 24, "model": "auth.permission", "fields": { "codename": "delete_logentry", "name": "Can delete log entry", "content_type": 8 } } ]""" x = json.loads(x) f = csv.writer(open("test.csv", "wb+")) # Write CSV Header, If you dont need that, remove this line f.writerow(["pk", "model", "codename", "name", "content_type"]) for x in x: f.writerow([x["pk"], x["model"], x["fields"]["codename"], x["fields"]["name"], x["fields"]["content_type"]]) we will get output as: pk,model,codename,name,content_type 22,auth.permission,add_logentry,Can add log entry,8 23,auth.permission,change_logentry,Can change log entry,8 24,auth.permission,delete_logentry,Can delete log entry,8by the way: i guess that we can do also like so:
a. install the pandas package.
running the following command to install the pandas package under Windows:
With the pandas library, this is as easy - we can go and use two commands
pandas.read_json()To convert a JSON string to a pandas object (either a series or dataframe).
Then, assuming the results were stored as df:
df.to_csv()Which can either return a string or write directly to a csv-file.
Based on the verbosity of previous answers, we should all thank pandas for the shortcut.
what do you think!?
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python