[英]How to make a new file based on the updated version of geoJSON file in python?
我一直在嘗試更新一個大型 geoJSON 文件,我想將所有 NAME_1 更新為小寫並將其保存在一個新文件中,有什么想法嗎?
這是我的 geoJSON 文件的一部分 有大量坐標,所以我只是將其切斷並使其成為....以簡化此處的帖子 對於 NAME_1 變量實際上大約有 32 個,我切斷了兩個只是為了簡化目的,但我需要更新所有 32
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
......
]
]
]
]
},
"properties": {
"GID_0": "IND",
"NAME_0": "India",
"GID_1": "IND.1_1",
"NAME_1": "Andaman and Nicobar",
"VARNAME_1": "Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare",
"NL_NAME_1": "",
"TYPE_1": "Union Territor",
"ENGTYPE_1": "Union Territory",
"CC_1": "",
"HASC_1": "IN.AN"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
......
]
]
]
]
},
"properties": {
"GID_0": "IND",
"NAME_0": "India",
"GID_1": "IND.2_1",
"NAME_1": "Andhra Pradesh",
"VARNAME_1": "",
"NL_NAME_1": "",
"TYPE_1": "State",
"ENGTYPE_1": "State",
"CC_1": "",
"HASC_1": "IN.AP"
}
我的代碼:
import sys
from geojson import Feature, Point, FeatureCollection, Polygon
import json
import geojson
import pprint
import pandas as pd
import re
import os
#print(sys.version)
with open('gadm36_IND_1.json', 'r') as data_file:
data = json.load(data_file)
for feature in data['features']:
#change all the Name_1 into lowercase
name1 = feature['properties']['NAME_1']
for f in re.findall("([A-Z]+)", name1):
name1 = name1.replace(f, f.lower())
with open('lower.json', 'w+') as data_file:
json.dump(data, data_file, indent=2)
請快速幫助嘿嘿嘿
您需要在原始data['features']
中設置值。
因此,在name1 = name1.replace(f, f.lower())
行中替換為feature['properties']['NAME_1'] = name1.replace(f, f.lower())
。
另一種方法是在第二個循環中添加這一行: feature['properties']['NAME_1'] = name1
。
假設您要轉換特征數組中所有對象的鍵NAME_1
的值。
import sys
import json
import re
import os
with open('gadm36_IND_1.json', 'r') as data_file:
data = json.load(data_file)
for feature in data['features']:
# change all the Name_1 into lowercase
feature['properties']['NAME_1'] = feature['properties']['NAME_1'].lower()
with open('lower.json', 'w+') as data_file:
json.dump(data, data_file, indent=2)
此代碼將鍵NAME_1
的值轉換為小寫並將新數據寫入文件lower.json
下面是我的lower.json
樣子:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"GID_0": "IND",
"NAME_0": "India",
"GID_1": "IND.1_1",
"NAME_1": "andaman and nicobar",
"VARNAME_1": "Andaman & Nicobar Islands|Andaman et Nicobar|Iihas de Andama e Nicobar|Inseln Andamanen und Nikobare",
"NL_NAME_1": "",
"TYPE_1": "Union Territory",
"ENGTYPE_1": "Union Territory",
"CC_1": "",
"HASC_1": "IN.AN"
}
},
{
"type": "Feature",
"properties": {
"GID_0": "IND",
"NAME_0": "India",
"GID_1": "IND.2_1",
"NAME_1": "andhra pradesh",
"VARNAME_1": "",
"NL_NAME_1": "",
"TYPE_1": "State",
"ENGTYPE_1": "State",
"CC_1": "",
"HASC_1": "IN.AP"
}
}
]
}
希望能幫助到你!
使用正則表達式
with open('gadm36_IND_1.json') as data_file:
Json_Str = data_file.read()
NAME_REGEX = re.compile(r'\"NAME_1\":\s*\"(.*?)\"')
for name in NAME_REGEX.findall(Json_Str):
Json_Str = Json_Str.replace(name , name.lower())
現在您有一個修改后的 Json 字符串,您可以輕松地將其轉換為 json obj
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.