I need to write a string from HTTP into CSV file.
My columns have to be: LATITUDE,LONGITUDE, OSM_ID, HIGHWAY, UPDATED_AT
This is a sample of the output of HTTP link starting from top:
{
"datetime": "2018-06-08T08:26:09.375Z",
"success": true,
"bbox": {
"xmin": "12.335513",
"ymin": "42.035682",
"xmax": "12.758896",
"ymax": "42.050826"
},
"data": [
{
"aggregate_id": 30201274,
"ppe": 0.316954620298806,
"geom": {
"type": "Point",
"coordinates": [
12.532972800901,
42.045435384225
]
},
"osm_id": "37015042",
"highway": "motorway",
"updated_at": "2018-01-20T03:27:11.047Z"
},
{
"aggregate_id": 30201275,
"ppe": 0.318124963244448,
"geom": {
"type": "Point",
"coordinates": [
12.5329908742,
42.045615145535
]
},
"osm_id": "37015042",
"highway": "motorway",
"updated_at": "2018-01-20T03:27:11.047Z"
},
{
"aggregate_id": 30201276,
"ppe": 0.204792151096739,
"geom": {
"type": "Point",
"coordinates": [
12.533008947499,
42.045794906844
]
},
"osm_id": "37015042",
"highway": "motorway",
"updated_at": "2018-01-20T03:27:11.047Z"
},
{
"aggregate_id": 30201277,
"ppe": 0.194797261691664,
"geom": {
"type": "Point",
"coordinates": [
12.533030586679,
42.045974206816
]
},
"osm_id": "37015042",
"highway": "motorway",
"updated_at": "2018-01-20T03:27:11.047Z"
}
]
}
Each row is separated by ','.
I wrote this code
import pandas as pd
import csv
import urllib.request
from urllib.request import urlopen
CSV_URL = 'http://www.smartroadsense.it/bb/12.335513/42.035682/12.758896/42.050826'
request = urllib.request.Request(CSV_URL)
response = urllib.request.urlopen(request)
response.read().decode('utf-8')
#write into csv
colNames = ["longitude","latitude","ppe","osm_id","highway","updated_at"]
data = pd.read_csv(CSV_URL, names=colNames, sep=',')
The problem is how to split the string from http into rows. Someone can help me?
machine-learning- kindly do not spam the tag (edited & removed).json.load()to parse it into a Python dict, then build your csv from this dict (you don't need panda for this, the stdlib'scsvmodule will be enough).