Search code examples
pythonjsoncsvdictionarytabulator

Python delimiters - from grib to json through csv - Copernicus API


After downloading data from the Copernicus API I'm receiving a .grib file which after conversion to .csv returns with these set :

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

What I want next is to json dictionary with Latitude, Longitude and Value.

Here's what I did until now :

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')

The problem is that after changing delimiter between Latitude, Longitude and Value I get double comma ',,' :

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

And (I think) because of that my json looks like this :

{
    "   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
    },

How to change this code to receive correct json file ?

What I want to achieve eventually is a simple dictionary which looks something like this :

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

Solution

  • Try this:

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

    Another solution:

    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))
    

    Output:

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