防止Python CSV到JSON for循环迭代覆盖前一个条目



我有一个非常基本的Python For语句,我正在使用它来尝试将CSV文件重新映射为geoJSON格式。我的脚本看起来像这样:

def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {
"type": "FeatureCollection",
"features": []
}
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": []
},
"properties": {}
}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)

# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
feature['geometry']['coordinates'] = [float(rows['s_dec']),float(rows['s_ra'])]
feature['properties'] = rows
data['features'].append(feature)
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))

但是,这会导致每个新行条目覆盖前一个。我的输出如下所示:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-67.33190277777777,
82.68714791666666
]
},
"properties": {
"dataproduct_type": "image",
"s_ra": "82.68714791666666",
"s_dec": "-67.33190277777777",
"t_min": "59687.56540044768",
"t_max": "59687.5702465162",
"s_region": "POLYGON 82.746588309 -67.328433557 82.78394862 -67.338513769"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-67.33190277777777,
82.68714791666666
]
},
"properties": {
"dataproduct_type": "image",
"s_ra": "82.68714791666666",
"s_dec": "-67.33190277777777",
"t_min": "59687.56540044768",
"t_max": "59687.5702465162",
"s_region": "POLYGON 82.746588309 -67.328433557 82.78394862 -67.338513769"
}
}
]}

有什么想法我在这里做错了吗?

我没有源文件来轻松测试这段代码,但我认为你的问题来自于对象的浅拷贝和深拷贝。

一种可能的方式来确保深度拷贝正在通过json。加载/json。转储循环。这可能不是很有效,所以如果执行时间是一个问题,请随意寻找另一种方法来生成深度拷贝。

feature_string = json.dumps(feature)
for row in csvReader:
buf = json.loads(feature_string)
buf["geometry"]["coordinates"] = [
float(rows["s_dec"]),
float(rows["s_ra"]),
]
buf["properties"] = row
data["features"].append(buf)

保存到文件,可以直接使用json.dump:

with open("json_output.json", encoding="utf-8", mode="w") as fout:
json.dump(data, fout, indent=4)

每条记录需要一个单独的数据结构。否则,特性列表将只包含对同一字典的重复引用。

def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {
"type": "FeatureCollection",
"features": []
}
# -- cut this ---
#    feature = {
#            "type": "Feature",
#            "geometry": {
#                    "type": "Point",
#                    "coordinates": []
#            },
#            "properties": {}
#    }
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)

# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# -- and paste it here --
feature = {
...
}
feature['geometry']['coordinates'] = [float(rows['s_dec']),float(rows['s_ra'])]
feature['properties'] = rows
data['features'].append(feature)

(代码可以稍微简化一下)

最新更新