通过csv哥白尼API从grib到json的Python分隔符

2024-06-06 15:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

从哥白尼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

Tags: 文件csvinjsondatavalueaswith
1条回答
网友
1楼 · 发布于 2024-06-06 15:14:02

试试这个:

import json

with open('data.csv') as fp, open('data.json', 'w') as fw:
    columns = fp.readline().strip().split(',')
    data = [line.strip().split() for line in fp if ',' not in line]
    res = [dict(zip(columns, x)) for x in data]
    json.dump(res, fw)

data.json

[
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.7973606873e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.7972239685e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8023333740e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8015911865e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8040211487e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8019508362e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8189350891e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8173139954e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8369824219e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8324902344e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8529223633e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8480590820e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.250",
    "Value": "2.8735998535e+02"
  },
  {
    "Latitude": "52.250",
    "Longitude": "-7.000",
    "Value": "2.8681311035e+02"
  }
]

另一种解决方案:

import json
from collections import defaultdict

with open('data.csv') as fp, open('data.json', 'w') as fw:
    columns = fp.readline().strip().split(',')
    res = defaultdict(list)
    for line in fp:
        if ',' not in line:
            x, y, z = line.strip().split()
            res[columns[0]].append(x)
            res[columns[1]].append(y)
            res[columns[2]].append(z)
print(dict(res))

输出:

{'Latitude': ['52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250', '52.250'], ' Longitude': ['-7.250', '-7.000', '-7.250', '-7.000', '-7.250', '-7.000', '-7.250', '-7.000', '-7.250', '-7.000', '-7.250', '-7.000', '-7.250', '-7.000'], ' Value': ['2.7973606873e+02', '2.7972239685e+02', '2.8023333740e+02', '2.8015911865e+02', '2.8040211487e+02', '2.8019508362e+02', '2.8189350891e+02', '2.8173139954e+02', '2.8369824219e+02', '2.8324902344e+02', '2.8529223633e+02', '2.8480590820e+02', '2.8735998535e+02', '2.8681311035e+02']}

相关问题 更多 >