Python map() function - ValueError: could not convert string to float

Python map() function - ValueError: could not convert string to float

我正在尝试 一个 csv 文件到 geoJSON。调用 map() 函数抛出 ValueError: could not convert string to float: 'latitude'。 这就是我现在所在的位置。无法弄清楚代码有什么问题。

CSV 样本:

id    name  host_id  host_name  neighbourhood_group  neighbourhood  latitude  longitude...
2314  Flat  3242     John       Someplace            Anotherplace   41.384..  41.384..

代码:

import csv, json
from geojson import Feature, FeatureCollection, Point

features = []
with open('listings_04-15.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for id, name, host_id, host_name, neighbourhood_group, neighbourhood, latitude, longitude, room_type, price, minimum_nights, number_of_reviews, last_review, reviews_per_month, calculated_host_listings_count, availability_365 in reader:
        latitude, longitude = map(float, (latitude, longitude))
        features.append(
            Feature(
                geometry = Point((longitude, latitude)),
                properties = {
                    'name': name,
                    'host_name': host_name,
                    'neighbourhood_group': neighbourhood_group,
                    'neighbourhood': neighbourhood,
                    'room_type': room_type,
                    'price': price,
                    'minimum_nights': minimum_nights,
                    'number_of_reviews': number_of_reviews,
                    'last_review': last_review,
                    'reviews_per_month': reviews_per_month,
                    'availability_365': availability_365
                }
            )
        )

collection = FeatureCollection(features)
with open('listings_04-15.geojson', "w") as f:
    f.write('%s' % collection)

您的代码正在读取 csv 文件的 header,试图对其进行解释。只需添加行

next(reader)

for 循环之前,你应该没问题。