在Python中将csv转换为geojson

在Python中将csv转换为geojson,python,csv,geojson,Python,Csv,Geojson,我正在尝试用python将一个文件转换为Geojson,我有以下代码。我曾尝试向代码中添加行索引,但错误仍然相同 import csv import json from collections import OrderedDict li = [] with open('sample.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') for lat,long in reader:

我正在尝试用python将一个文件转换为Geojson,我有以下代码。我曾尝试向代码中添加行索引,但错误仍然相同

import csv
import json
from collections import OrderedDict

li = []
with open('sample.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for lat,long in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(lat), float(long)]
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
我在错误下面

"too many values to unpack (expected 2)"
样本数据

event_id    time_0  time_1  signal  description signal_name_value_desc  product long    lat
a   6/30/2018 18:39 6/30/2018 18:39 1   description1    signal_name1    product-1   -84.52694   46.931625
a   6/30/2018 18:39 6/30/2018 18:39 1   description1    signal_name1    Product - 1 -84.52684   46.931725
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25526333    42.71689167
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25524667    42.71689333
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25519167    42.716895
b   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25505167    42.71690833
b   10/15/2018 21:12    10/15/2018 21:13    1   description1    signal_name1    Product - 2 -94.25531167    42.71687167
b   10/15/2018 21:12    10/15/2018 21:13    1   description1    signal_name1    
这是我期望的结果

{
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -84.52694,46.931625 ]
    },
    "properties": {
    "event_id":"a",
    "time_0":"6/30/2018 18:39",
    "time_1":"6/30/2018 18:39",
    "signal":"1",
    "description":"description1",
    "signal_name_value_desc":"signal_name1",
    "product":"product-1",
    }
  }

如何将csv转换为GeoJson。提前感谢

由于您声明.CSV文件中有13列,问题在于:

for lat,long in reader:
该行要求一行reader有两列。例如:

>>> lat,long = 1,2,3,4,5,6,7,8,9,10,11,12,13
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
您也可以只使用以下内容并索引所需的列:

for row in reader:
    lat = row[0]   # or whatever the columns really are
    long = row[1]
如果您的数据有如您所示的标题,下面是一个使用DictReader按名称引用列的示例

给定input.csv:

此代码:

import csv
import json
from collections import OrderedDict

li = []
with open('input.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(row['lat']), float(row['long'])]
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li

with open('output.json','w') as f:
    json.dump(d,f,indent=2)
生成output.json:


我的csv文件中大约有13列,您有没有一个示例,说明您对json结果的预期?您可以分享您的csv文件的示例吗?我已发布了示例数据。我无法上载csv文件。我尝试为列指定变量,但错误仍然存在same@anonymous13请用实际CSV编辑您的问题。例如,所示的没有逗号。还包括指示错误发生位置的完整回溯。我无法将csv添加到question@anonymous13不是整个CSV…只需从顶部剪切粘贴几行…更改任何敏感数据,但要正确获取样本格式。请参见我上面的input.csv示例。您的问题应该显示一个产生错误的列。谢谢。代码可以工作,但错误是因为我的lat或long列中有一个空值。如何从csv reader中的任何列中删除“NA”?我熟悉从数据帧中删除它。
event_id,time_0,time_1,signal,description,signal_name_value_desc,product,long,lat
a,6/30/2018 18:39,6/30/2018 18:39,1,description1,signal_name1,product-1,-84.52694,46.931625
import csv
import json
from collections import OrderedDict

li = []
with open('input.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(row['lat']), float(row['long'])]
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li

with open('output.json','w') as f:
    json.dump(d,f,indent=2)
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          46.931625,
          -84.52694
        ]
      }
    }
  ]
}