You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CSV转GeoJSON时如何去除坐标字段多余的双引号?

Fixing Extra Double Quotes in GeoJSON Coordinates from CSV Conversion

Hey there! Let's sort out that annoying extra double quote issue with your coordinates. Since you're new to Python, I'll keep this straightforward and actionable.

What's Probably Happening

Looking at your code snippet, you're using quotechar='|' in csv.DictReader, but it sounds like your CSV's coordinate field is wrapped in double quotes (like "123.45,67.89"). Since the reader isn't set to recognize " as a quote character, it's pulling those quotes into your string data—hence the extra quotes in your final GeoJSON.

Two Simple Fixes

1. Adjust the CSV Reader's Quote Character

If your CSV uses double quotes to wrap fields, just update the quotechar parameter to match:

csvReader = csv.DictReader(csvfile, delimiter=',', quotechar='"')

This tells the reader to automatically strip off those wrapping double quotes when parsing each field, so you'll get clean coordinate strings right off the bat.

2. Manually Strip Quotes from the Coordinate String

If you can't change the reader setup (maybe your CSV uses mixed quote types), explicitly remove the quotes when processing the coordinate field. Here's how to integrate that into your script:

import csv

def readCsvFile():
    features = []
    geoJson = {
        "type": "FeatureCollection",
        "features": features
    }
    
    with open('resources/data_export.csv', newline='') as csvfile:
        csvReader = csv.DictReader(csvfile, delimiter=',', quotechar='|')
        for row in csvReader:
            # Replace 'coordinates' with your actual column name from the CSV
            raw_coord_str = row['coordinates']
            # Strip leading/trailing double quotes
            cleaned_coord_str = raw_coord_str.strip('"')
            # Split into longitude and latitude, convert to numeric values
            longitude, latitude = map(float, cleaned_coord_str.split(','))
            
            # Build the GeoJSON feature object
            feature = {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [longitude, latitude]
                },
                "properties": {
                    # Add your other CSV columns here (adjust keys to match your data)
                    "site_name": row.get('site_name', '')
                }
            }
            features.append(feature)
    
    return geoJson
  • strip('"') only removes double quotes at the start and end of the string (safer than replace('"', ''), which would erase all quotes—even if they belonged in other fields).
  • Converting the split values to float ensures your coordinates are stored as numbers in the final GeoJSON, not strings.

Quick Debug Tip

Before diving into fixes, print out print(row['coordinates']) right after reading each row. This will show you exactly what raw data you're working with, making it easier to confirm where the quotes are coming from.

内容的提问来源于stack exchange,提问作者HoudaF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:20:18