【发布时间】:2018-04-09 14:59:46
【问题描述】:
我开始使用 Python3,我正在编写一个脚本来将 CSV 文件转换为 GeoJSON 文件。 转换有效,但我的坐标仍有问题,格式不正确。我需要删除双引号,但它不起作用。
这是我的部分代码:
def readCsvFile():
features=[]
geoJson={}
with open('resources/data_export.csv',newline= '') as csvfile:
csvReader = csv.DictReader(csvfile,delimiter=',',quotechar='|')
for row in csvReader:
features.append({'type': 'Feature',
'geometry': {
'type': 'Point', "coordinates" : [str(row['longitude']), str(row['latitude'])]}, 'properties': {'id': decodeString(row['id']), 'field1': decodeString(row['field1']), 'field2': decodeString(row['field2'])})
geoJson = {'type': 'FeatureCollection', 'features': features}
return json.dumps(geoJson)
我得到了这个结果:
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": ["16.394564", "48.246426"]}, "properties": {"id": "0", ...
虽然我应该得到:
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [16.394564, 48.246426]}, "properties": {"id": "0",
【问题讨论】:
-
在您的预期结果中,您的坐标列表中仍然有两个
",这是正确的吗? -
您确实意识到
json.dumps返回str对吧? -
是的,这正是我需要删除的内容
-
尝试发布你的
data_export.csv的前两行左右 -
我在回答中建议的浮点转换适用于您提供的行..(刚刚在 Python 3.6 上测试过)我认为您可能在其他地方有无效行
标签: python json python-3.x csv geojson