【发布时间】:2020-10-21 18:59:35
【问题描述】:
从 Copernicus API 下载数据后,我收到一个 .grib 文件,该文件在转换为 .csv 后返回这些集合:
Latitude, Longitude, Value
52.250 -7.250 2.7973606873e+02
52.250 -7.000 2.7972239685e+02
Latitude, Longitude, Value
52.250 -7.250 2.8023333740e+02
52.250 -7.000 2.8015911865e+02
Latitude, Longitude, Value
52.250 -7.250 2.8040211487e+02
52.250 -7.000 2.8019508362e+02
Latitude, Longitude, Value
52.250 -7.250 2.8189350891e+02
52.250 -7.000 2.8173139954e+02
Latitude, Longitude, Value
52.250 -7.250 2.8369824219e+02
52.250 -7.000 2.8324902344e+02
Latitude, Longitude, Value
52.250 -7.250 2.8529223633e+02
52.250 -7.000 2.8480590820e+02
Latitude, Longitude, Value
52.250 -7.250 2.8735998535e+02
52.250 -7.000 2.8681311035e+02
接下来我想要的是带有纬度、经度和值的 json 字典。
这是我到目前为止所做的:
import csv, json
csvPath = "sample_area.csv"
editedCsv = "sample_edited.csv"
jsonPath = "formatted.json"
with open(csvPath, 'r') as f_in, open(editedCsv, 'w') as f_out:
f_out.write(next(f_in))
[f_out.write(','.join(line.split()) + '\n') for line in f_in]
data = {}
with open(csvPath) as csvFile:
csvReader = csv.DictReader(csvFile)
for csvRow in csvReader:
Latitude = csvRow["Latitude"]
data[Latitude] = csvRow
print(data)
with open(jsonPath, "w") as jsonFile:
jsonFile.write(json.dumps(data, indent=4))
print('\n', editedCsv.lower(), "has been edited and", jsonPath, "has been created", '\n')
问题是在纬度、经度和值之间更改分隔符后,我得到双逗号',,':
Latitude, Longitude, Value
52.250,-7.250,2.7973606873e+02
52.250,-7.000,2.7972239685e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8023333740e+02
52.250,-7.000,2.8015911865e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8040211487e+02
52.250,-7.000,2.8019508362e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8189350891e+02
52.250,-7.000,2.8173139954e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8369824219e+02
52.250,-7.000,2.8324902344e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8529223633e+02
52.250,-7.000,2.8480590820e+02
Latitude,,Longitude,,Value
52.250,-7.250,2.8735998535e+02
52.250,-7.000,2.8681311035e+02
而且(我认为)因为我的 json 看起来像这样:
{
" 52.250 -7.250 2.7973606873e+02": {
"Latitude": " 52.250 -7.250 2.7973606873e+02",
" Longitude": null,
" Value": null
},
" 52.250 -7.000 2.7972239685e+02": {
"Latitude": " 52.250 -7.000 2.7972239685e+02",
" Longitude": null,
" Value": null
},
"Latitude": {
"Latitude": "Latitude",
" Longitude": " Longitude",
" Value": " Value"
},
" 52.250 -7.250 2.8023333740e+02": {
"Latitude": " 52.250 -7.250 2.8023333740e+02",
" Longitude": null,
" Value": null
},
" 52.250 -7.000 2.8015911865e+02": {
"Latitude": " 52.250 -7.000 2.8015911865e+02",
" Longitude": null,
" Value": null
},
" 52.250 -7.250 2.8040211487e+02": {
"Latitude": " 52.250 -7.250 2.8040211487e+02",
" Longitude": null,
" Value": null
},
如何更改此代码以接收正确的 json 文件?
我最终想要实现的是一个简单的字典,看起来像这样:
Latitude, Longitude, Value
52.250,-7.250,2.7973606873e+02
52.250,-7.000,2.7972239685e+02
52.250,-7.250,2.8023333740e+02
52.250,-7.000,2.8015911865e+02
52.250,-7.250,2.8040211487e+02
52.250,-7.000,2.8019508362e+02
52.250,-7.250,2.8189350891e+02
52.250,-7.000,2.8173139954e+02
52.250,-7.250,2.8369824219e+02
52.250,-7.000,2.8324902344e+02
52.250,-7.250,2.8529223633e+02
52.250,-7.000,2.8480590820e+02
52.250,-7.250,2.8735998535e+02
52.250,-7.000,2.8681311035e+02
【问题讨论】:
-
“我最终想要实现的是一个看起来像这样的简单字典”它不是字典,它是一个 csv 文件。你想输出为 json 还是 csv?
标签: python json csv dictionary tabulator