【发布时间】:2018-02-02 16:11:26
【问题描述】:
我需要一个 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=''? -
您说输出与上面的格式不匹配,但您将完全不同的格式附加到列表中。你在纠结什么?
-
是 csv 中的纬度、经度列吗?
-
我建议您将 csv 的内容发布给其他遇到相同问题/担忧的人