我需要将 HTTP 中的字符串写入 CSV 文件。
我的列必须是:LATITUDE、LONGITUDE、OSM_ID、HIGHWAY、UPDATED_AT
这是从顶部开始的 HTTP 链接的输出示例:
{
"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"
}
]
}
每行用“,”分隔。
这段代码是我写的
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=',')
问题是如何将 http 中的字符串拆分为行。有人可以帮助我吗?
最佳答案
这是使用 pandas
和 requests
的简单实现:
import pandas as pd
import requests
url = 'http://www.smartroadsense.it/bb/12.335513/42.035682/12.758896/42.050826'
response = requests.get(url).json()
df = pd.DataFrame(response['data'])
您可以从 geom
列中提取经度
和纬度
,如下所示:
df['longitude'] = df.apply(lambda row: dict(row['geom'])['coordinates'][0], axis=1)
df['latitude'] = df.apply(lambda row: dict(row['geom'])['coordinates'][1], axis=1)
最后,将所需的列保存到 csv
文件中,如下所示:
df[['latitude', 'longitude', 'osm_id', 'highway', 'updated_at']].to_csv('output.csv', index=False)
关于Python:从http到csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759046/