Python:在非结构化 JSON 中将 'WASHINGTON' 的所有实例更改为 'District of Columbia'
Python: Change all instances of 'WASHINGTON' to 'District of Columbia' in Unstructured JSON
我有一些非结构化的 JSON 看起来像这样:
{
"Entities": [{
"BeginOffset": 19, "EndOffset": 32, "Score": 2.0,
"Text": "WASHINGTON", "Type": "LOCATION"
}, {
"BeginOffset": 33, "EndOffset": 35, "Score": 1,
"Text": "Ha", "Type": "LOCATION"
}, {
"BeginOffset": 36, "EndOffset": 39, "Score": 2.2,
"Text": "JAN", "Type": "LOCATION"
}],
"File": "sample.txt",
"Line": 11
}
我将其上传到 Elasticsearch 以便在 Kibana 中使用 - 但地理标记功能给出了 WASHINGTON
华盛顿州的坐标,而不是我们尊敬的首都哥伦比亚特区。我需要直流坐标。
上面的 JSON 一直是 WASHINGTON
- 我想将每个 WASHINGTON
更改为 District of Columbia
。我正在使用 Google 地图 API 对 JSON:
中的位置进行地理标记
if str(json_package['document']['Entities'][0]['Type']) == "LOCATION":
geocode_result = gmaps.geocode(
json_package['document'] ['Entities'][0]['Text'])
lat_long = {
'lat': geocode_result[0]['geometry']['location']['lat'],
'lon': geocode_result[0]['geometry']['location']['lng']
}
# print(lat_long)
我该如何更新?上面的示例显示了我如何将 lng
的所有实例更改为 lon
,但同样的事情对城市不起作用。我一直在off this link
我还意识到更改文本必须在实际地理编码之前发生
听起来您应该能够查看每个实体并根据需要更新它们:
# Let data = your unstructured JSON object
for entity in data['Entities']:
if entity['Type'] == 'LOCATION' and entity['Text'] == 'WASHINGTON':
entity['Text'] = 'District of Columbia'
我有一些非结构化的 JSON 看起来像这样:
{
"Entities": [{
"BeginOffset": 19, "EndOffset": 32, "Score": 2.0,
"Text": "WASHINGTON", "Type": "LOCATION"
}, {
"BeginOffset": 33, "EndOffset": 35, "Score": 1,
"Text": "Ha", "Type": "LOCATION"
}, {
"BeginOffset": 36, "EndOffset": 39, "Score": 2.2,
"Text": "JAN", "Type": "LOCATION"
}],
"File": "sample.txt",
"Line": 11
}
我将其上传到 Elasticsearch 以便在 Kibana 中使用 - 但地理标记功能给出了 WASHINGTON
华盛顿州的坐标,而不是我们尊敬的首都哥伦比亚特区。我需要直流坐标。
上面的 JSON 一直是 WASHINGTON
- 我想将每个 WASHINGTON
更改为 District of Columbia
。我正在使用 Google 地图 API 对 JSON:
if str(json_package['document']['Entities'][0]['Type']) == "LOCATION":
geocode_result = gmaps.geocode(
json_package['document'] ['Entities'][0]['Text'])
lat_long = {
'lat': geocode_result[0]['geometry']['location']['lat'],
'lon': geocode_result[0]['geometry']['location']['lng']
}
# print(lat_long)
我该如何更新?上面的示例显示了我如何将 lng
的所有实例更改为 lon
,但同样的事情对城市不起作用。我一直在off this link
我还意识到更改文本必须在实际地理编码之前发生
听起来您应该能够查看每个实体并根据需要更新它们:
# Let data = your unstructured JSON object
for entity in data['Entities']:
if entity['Type'] == 'LOCATION' and entity['Text'] == 'WASHINGTON':
entity['Text'] = 'District of Columbia'