简体   繁体   中英

How can I automatically put data into code lines?

I have a GEOJSON code that I got from Statistics Canada and I'm trying to input some data in it.

Here is my geojson code

{ "type": "Feature","properties": {"name": "001", "ivl_a": 1.1, "ivl_f": 0.3, "differentiel": 1.5...
{ "type": "Feature","properties": {"name": "002", "ivl_a": 1.1, "ivl_f": 0.3, "differentiel": 0.4...
{ "type": "Feature","properties": {"name": "003", "ivl_a": 1.1, "ivl_f": 0.3, "differentiel": 0.4...

I want to replace the data for ivl_a, ivl_f and differentiel with data that I have in Excel columns.

How can I do this?

Perhaps this helps enough to guide you although i'm not sure if i have got all the details right.

Since you didn't state the details I've made some assumptions :

  • your geojson data is a list of json objects, each with a 'properties' key
  • you are updating feature properties based using a lookup value of 'name' in the spreadsheet which is included in each feature properties

I've setup a csv structure (without header) of : name, ivl_a, ivl_f, differentiel

In terms of reading/writing the geojson data I've left this as an exercise for you (since the example data above is incomplete)

import csv


def read_csv(csv_filename):
    # assuming csv of (without header)
    #  name, ivl_a, ivl_f, differentiel

    lookup = {}

    with open(csv_filename) as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            name = row[0]
            lookup[name] = {
                'lvl_a': row[1],
                'lvl_f': row[2],
                'differentiel': row[3]
            }

    return lookup


def main():
    lookup = read_csv('data.csv')

    # todo: read the data into list of dicts
    features = []

    for feature in features:
        props = feature['properties']
        name = props['name']
        props['lvl_a'] = lookup[name]['lvl_a']
        props['lvl_f'] = lookup[name]['lvl_f']
        props['differentiel'] = lookup[name]['differentiel']

    # todo: save


if __name__ == '__main__':
    main()

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM