【问题标题】:Python delimiters - from grib to json through csv - Copernicus APIPython 分隔符 - 从 grib 到 json 通过 csv - Copernicus API
【发布时间】: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


【解决方案1】:

试试这个:

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']}

【讨论】:

  • 谢谢@komatiraju032 这确实解决了逗号问题。仍然 - 我需要实现的是一本包含纬度、经度和值列的字典,而不是字典列表。抱歉,如果我之前没有足够全面地解释它。
  • @Geophysicist 你能发布预期的输出吗
  • 当然@komatiraju032。我确实已经更新了主线程。
  • 这不是字典,你能澄清一下吗?
  • 你是对的 - 我实际上认为我最好使用字典列表。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多