我需要一份Python脚本将CSV数据转换为GeoJSON输出。输出应该符合以下格式:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -85.362709,40.466442 ]
},
"properties": {
"weather":"Overcast",
"temp":"30.2 F"
}
}
]
}
我正在使用这个脚本来运行进程,但它没有产生期望的输出:
import csv, json
li = []
with open('CurrentObs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
li.append({
"latitude": latitude,
"longitude": longitude,
"weather": weather,
"temp": temp,
"geo": {
"__type": "GeoPoint",
"latitude": latitude,
"longitude": longitude,
}
})
with open("GeoObs.json", "w") as f:
json.dump(li, f)
非常感谢任何帮助!
CurrentObs.csv
里面是什么?你确定需要newline=''
吗? - Paulo Scardine