Hướng dẫn python json replace value by key - python json thay thế giá trị bằng khóa

Đối với tệp geojson có tên data như sau:

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A0"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A4"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ]
            }
        }
    ]
}

Tôi hy vọng sẽ nhận ra các thao tác sau đây đến data:

  1. Thay thế khóa EntityHandle bằng Name;
  2. thay thế giá trị của ____ 6 từ số Hex bằng 'sf_001', 'sf_002', 'sf_003', v.v.;
  3. Thay thế giá trị của ____ 10
    {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbPolyline",
                    "Name": "sf_001"
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [ [
                        [3220.136443006845184, 3001.530372177397112],
                        [3847.34171007254281, 3000.86074447018018],
                        [3847.34171007254281, 2785.240077064262096],
                        [3260.34191304818205, 2785.240077064262096],
                        [3260.34191304818205, 2795.954148466309107]
                    ] ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbPolyline",
                    "Name": "sf_002"
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [ [
                        [3611.469650131302842, 2846.845982610575902],
                        [3695.231030111376185, 2846.845982610575902],
                        [3695.231030111376185, 2785.240077064262096],
                        [3611.469650131302842, 2785.240077064262096],
                        [3611.469650131302842, 2846.845982610575902]
                    ] ]
                }
            }
        ]
    }
    
    1 bằng
    {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbPolyline",
                    "Name": "sf_001"
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [ [
                        [3220.136443006845184, 3001.530372177397112],
                        [3847.34171007254281, 3000.86074447018018],
                        [3847.34171007254281, 2785.240077064262096],
                        [3260.34191304818205, 2785.240077064262096],
                        [3260.34191304818205, 2795.954148466309107]
                    ] ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbPolyline",
                    "Name": "sf_002"
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [ [
                        [3611.469650131302842, 2846.845982610575902],
                        [3695.231030111376185, 2846.845982610575902],
                        [3695.231030111376185, 2785.240077064262096],
                        [3611.469650131302842, 2785.240077064262096],
                        [3611.469650131302842, 2846.845982610575902]
                    ] ]
                }
            }
        ]
    }
    
    2;
  4. Thay thế hai khung hình vuông của ____ 13 bằng ba dấu ngoặc vuông.

Điều này là đầu ra dự kiến:

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}

Tôi là người mới trong thao tác tập tin

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
4 bằng Python. Xin hãy giúp tôi, cảm ơn tại trước.

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']

Result:

Traceback [most recent call last]:

  File "", line 2, in 
    feature['properties']['EntityHandle'] = feature['properties']['Name']

KeyError: 'Name'

Trong bài đăng này:

  1. Giới thiệu

  2. Tệp JSON

  3. Tệp CSV

  4. Kịch bản Python

  5. Viết kịch bản Python

Tạo tập lệnh Python liên quan đến rất nhiều thử nghiệm và lỗi kể từ khi đã đề cập trước đây, việc làm tổ trong tệp JSON đã gây ra các phương pháp thông thường mà tôi sử dụng để tìm/thay thế các giá trị bằng cách sử dụng Python bị lỗi. Suy nghĩ đầu tiên của tôi là lặp lại thông qua tệp JSON và sử dụng câu lệnh IF để nói data0, data1 là một trong những khóa/giá trị từ CSV. Tuy nhiên, với phương pháp đó, dường như Python chỉ đi qua lớp ngoài cùng của tệp JSON. Mặc dù tôi chắc chắn có một cách tốt hơn để thực hiện điều này-tôi đã giữ cho data2 để thay thế các giá trị trong tệp JSON, nhưng đã chỉ định vị trí chính xác bên trong tệp để tìm ra X X.

Đây là nơi mà thử nghiệm và lỗi xuất hiện. Đối với mỗi giá trị cần được thay thế, tôi đã sử dụng các câu lệnh in để thu hẹp vị trí của nơi tôi đang ở trong tệp. Định dạng tôi đã sử dụng là

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
6 theo sau là tên của từng lớp lồng nhau trong ngoặc data4. Điều này tương tự như sử dụng các chỉ mục để tìm các giá trị trong một danh sách. Một số lớp của tệp JSON chứa nhiều lớp cùng tên, điều đó có nghĩa là sử dụng data5 sẽ hoạt động. Để tìm ra cách để có được nơi tôi cần, tôi đã kiểm tra loại nơi tôi đang sử dụng data6.

Tệp JSON

Tệp CSV

Further down, both key:value pairs
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
7 and
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
8 needed to be updated in their own blocks- and it was very important that the keys were matched with their corresponding values.
I discovered that the nested format of the JSON file made it difficult to search for values in the file using python. Additionally-some values were of the type list and some were type dictionaries. To find the work around for this, see the “Writing the Python Script” section.

Tệp CSV

Kịch bản Python

Kịch bản Python

Dòng 4-12: Thiết lập

Trong phần đầu tiên của tập lệnh Python, tôi nhập các mô -đun cần thiết [CSV và JSON] và khởi tạo 2 danh sách và tổng cộng 6 biến. Một danh sách sẽ chứa các khóa từ CSV, dữ liệu còn lại sẽ chứa các giá trị. Điều này là do đó nội dung của CSV có thể dễ dàng được xử lý trong tập lệnh. Biến

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
9 được đặt thành 1, trong khi phần còn lại là 0-đây phát huy tác dụng sau đó trong tập lệnh và đảm bảo rằng các lần lặp hoạt động chính xác.

Dòng 14-19: Chấp nhận đầu vào từ CSV và thêm các giá trị vào danh sách

Khi mọi thứ được thiết lập, tệp CSV phải được mở và các khóa/giá trị được đặt vào danh sách tương ứng của chúng. Đầu tiên, dòng 14 mở tệp CSV đầu vào.csv, là biến

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
0 với quyền đọc
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
1. Sau đó, mô -đun CSV được sử dụng để đọc tệp và chỉ định rằng dấu phân cách được sử dụng là dấu phẩy,. Dòng 16 chỉ định để bỏ qua dòng đầu tiên trong tệp CSV vì tôi chứa các tiêu đề cột. Cuối cùng, các dòng 17-29 lặp qua các hàng trong CSV và thêm các phím vào
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
2 và các giá trị vào
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
3. Điều này được thực hiện bằng cách chỉ định rằng các khóa nằm trong chỉ số 0 của mỗi hàng,
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
4, trong khi các giá trị nằm trong 1 chỉ số
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
5.

Các dòng 22-34: Mở tệp JSON gốc và các khóa cập nhật trong tệp

Dòng 22 mở tệp JSON giống như cách mở CSV, nhưng ở đây, mô -đun JSON được sử dụng để tải nội dung là

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
6. Tiếp theo, 3 vòng riêng cho các vòng được sử dụng để thay thế các khóa trong tệp JSON. Đầu tiên cho Loop thay thế các khóa cùng nhau trong một khối lồng nhau trong tệp JSON, mà không được ghép nối với các giá trị của chúng. Đây là vòng duy nhất cho vòng lặp sử dụng 2 biến để lặp lại.
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
7 lặp lại qua
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
8 và vì
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
7 được đặt thành bằng 0, lần lặp bắt đầu ở phím đầu tiên trong
import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
8.
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
9, được đặt thành bằng 1, lặp lại thông qua các giá trị ban đầu trong tệp JSON đang được thay thế trong vòng lặp này cho vòng lặp. Tệp JSON ban đầu có 2 giá trị cần được thay thế bằng cách sử dụng vòng lặp này cho vòng lặp:
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
5 và
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
6. Điều này được chỉ định trong dòng 25 là
Traceback [most recent call last]:

  File "", line 2, in 
    feature['properties']['EntityHandle'] = feature['properties']['Name']

KeyError: 'Name'
4. Vì vậy, khi
{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}
9 được tăng thêm 1 trong vòng lặp For, phím cũ được thay thế bằng khóa mới tương ứng. Thứ hai và thứ ba cho các vòng lặp thay thế các khóa khác trong tập lệnh khi chúng được phân tách bằng các giá trị tương ứng của chúng. Điều này có nghĩa là vị trí chính xác của giá trị được thay thế được chỉ định, thay vì sử dụng câu lệnh IF như vòng lặp đầu tiên.
The second and third for loops replace the other keys in the script when they are separated with their respective values. This means that the exact location of the value to be replaced is specified, instead of using an if statement like the first loop.

Dòng 37-41: Cập nhật các giá trị trong tệp

Các giá trị được thay thế trong tệp JSON tương tự như cách các khóa được thay thế bằng cách sử dụng các vòng lặp. Ở đây 2 vòng được sử dụng, mỗi vòng thay thế một giá trị trong tệp JSON tương ứng với các vòng thứ hai và thứ ba thay thế các khóa. Khi phần này của tập lệnh được chạy, các cặp giá trị: giá trị được nhóm lại với nhau trong tệp JSON được thay thế bằng các cặp khóa: giá trị mới.

Dòng 44-45: Viết vào tệp JSON mới

Cuối cùng, một tệp JSON mới, File mới. Sau đó, mô -đun JSON kết hợp nội dung của tệp JSON cũ

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
6 với nội dung được cập nhật
Traceback [most recent call last]:

  File "", line 2, in 
    feature['properties']['EntityHandle'] = feature['properties']['Name']

KeyError: 'Name'
6. Điều này tạo ra một tệp JSON mới với các giá trị được cập nhật trong khi tệp JSON gốc không bị ảnh hưởng.

Viết kịch bản Python

Tạo tập lệnh Python liên quan đến rất nhiều thử nghiệm và lỗi kể từ khi đã đề cập trước đây, việc làm tổ trong tệp JSON đã gây ra các phương pháp thông thường mà tôi sử dụng để tìm/thay thế các giá trị bằng cách sử dụng Python bị lỗi. Suy nghĩ đầu tiên của tôi là lặp lại thông qua tệp JSON và sử dụng câu lệnh IF để nói data0, data1 là một trong những khóa/giá trị từ CSV. Tuy nhiên, với phương pháp đó, dường như Python chỉ đi qua lớp ngoài cùng của tệp JSON. Mặc dù tôi chắc chắn có một cách tốt hơn để thực hiện điều này-tôi đã giữ cho data2 để thay thế các giá trị trong tệp JSON, nhưng đã chỉ định vị trí chính xác bên trong tệp để tìm ra X X.

Đây là nơi mà thử nghiệm và lỗi xuất hiện. Đối với mỗi giá trị cần được thay thế, tôi đã sử dụng các câu lệnh in để thu hẹp vị trí của nơi tôi đang ở trong tệp. Định dạng tôi đã sử dụng là

import json
from pprint import pprint

with open['data.geojson'] as f:
    data = json.load[f]

pprint[data]

for feature in data['features']:
    #print[feature]
    print[feature['properties']['EntityHandle']]

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']
6 theo sau là tên của từng lớp lồng nhau trong ngoặc data4. Điều này tương tự như sử dụng các chỉ mục để tìm các giá trị trong một danh sách. Một số lớp của tệp JSON chứa nhiều lớp cùng tên, điều đó có nghĩa là sử dụng data5 sẽ hoạt động. Để tìm ra cách để có được nơi tôi cần, tôi đã kiểm tra loại nơi tôi đang sử dụng data6.

Tôi phát hiện ra rằng các lớp có nhiều lớp trong đó là danh sách-điều đó có nghĩa là tôi có thể sử dụng chỉ mục của lớp tôi muốn đi vào như một phần của con đường tôi đang chỉ định. Ví dụ: data7 để nhập một lớp và data8 để nhập một lớp khác cả dưới JSON> Group2> Tài nguyên.

Lưu ý: Nếu bạn sử dụng mã Visual Studio để mở tệp JSON, bạn có thể thấy đường dẫn đến một giá trị ở đầu màn hình nếu bạn nhấp vào giá trị.Bạn có thể sử dụng đường dẫn này được phân tách bằng dấu ngoặc thay vì sử dụng các câu lệnh in để tìm đường dẫn.: If you use Visual Studio Code to open the JSON file, you can see the path to a value at the top of the screen if you click on a value. You can use this path separated by brackets instead of using print statements to find the path.

Bài Viết Liên Quan

Chủ Đề