Python 如何通过对单个数据字段的语言检测在json中添加数据字段(键值)

Python 如何通过对单个数据字段的语言检测在json中添加数据字段(键值),python,json,language-detection,Python,Json,Language Detection,我有天气警报数据,比如 "alerts": [ { "description": "There is a risk of frost (Level 1 of 2).\nMinimum temperature: ~ -2 \u00b0C", "end": 1612522800, "event": "frost",

我有天气警报数据,比如

"alerts": [
    {
        "description": "There is a risk of frost (Level 1 of 2).\nMinimum temperature: ~ -2 \u00b0C",
        "end": 1612522800,
        "event": "frost",
        "sender_name": "DWD / Nationales Warnzentrum Offenbach",
        "start": 1612450800
    },
    {
        "description": "There is a risk of widespread icy surfaces (Level 1 of 3).\ncause: widespread ice formation or traces of snow",
        "end": 1612515600,
        "event": "widespread icy surfaces",
        "sender_name": "DWD / Nationales Warnzentrum Offenbach",
        "start": 1612450800
    },
    {
        "description": "Es treten Windb\u00f6en mit Geschwindigkeiten um 55 km/h (15m/s, 30kn, Bft 7) aus \u00f6stlicher Richtung auf. In exponierten Lagen muss mit Sturmb\u00f6en bis 65 km/h (18m/s, 35kn, Bft 8) gerechnet werden.",
        "end": 1612587600,
        "event": "WINDB\u00d6EN",
        "sender_name": "DWD / Nationales Warnzentrum Offenbach",
        "start": 1612522800
    },
现在,我想在每个alert dict中添加一个键值对,其中包含“description”字段中的语言检测。我试过了,但是找不到正确的语法

import json
from langdetect import detect

with open("kiel.json", 'r') as f:
    data = json.loads(f.read())

data['ADDED_KEY'] = 'ADDED_VALUE'
#'ADDED_KEY' = 'lang' - should be added as a data field to EVERY alert
#'ADDED_VALUE' = 'en' or 'ger' - should be the detected language [via detect()] from data field 'description' of every alert 

with open("kiel.json", 'w') as f:
    f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
实际上,我刚刚在整个文件中添加了如下内容:

{
"ADDED_KEY": "ADDED_VALUE",
"alerts": [
    {
        "description": "There is a risk of frost (Level 1 of 2).\nMinimum temperature: ~ -2 \u00b0C",
        "end": 1612522800,
        "event": "frost",
        "sender_name": "DWD / Nationales Warnzentrum Offenbach",
        "start": 1612450800
    },
你能帮我以正确的方式完成代码,正确访问正确的数据字段吗

进一步:

现在出现的情况是,“警报”没有包含在数据字段中(例如,由于天气晴朗,没有发送警报数据时)——我还想生成JSON。我试过:

for item in data['alerts']:
    if 'alerts' not in data:
        continue
else:
    item['lang'] = detect(item['description'])
但如果没有我得到的“警报”数据字段

      for item in data['alerts']:
KeyError: 'alerts'
我怎样才能解决这个问题?“继续”不是正确的任务吗?或者我必须改变if-and-for循环?
又来了

您只需迭代字典键
警报
,并将键、值添加到每个
(这是一个字典)

数据[“警报”]中的项的

item[“ADDED_KEY”]=“ADDED_VALUE”
您只需迭代字典键
警报
,并将键、值添加到每个
(这是一个字典)

数据[“警报”]中的项的

项目[“添加的密钥”]=“添加的值”
以下作品。迭代警报并添加您提到的键/值

import json
from langdetect import detect

with open("kiel.json", 'r') as f:
    data = json.loads(f.read())

for item in data['alerts']:
    item['lang'] = detect(item['description']) 
#'ADDED_KEY' = 'lang' - should be added as a data field to EVERY alert
#'ADDED_VALUE' = 'en' or 'ger' - should be the detected language [via detect()] from data field 'description' of every alert 

with open("kiel.json", 'w') as f:
    f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))

以下作品。迭代警报并添加您提到的键/值

import json
from langdetect import detect

with open("kiel.json", 'r') as f:
    data = json.loads(f.read())

for item in data['alerts']:
    item['lang'] = detect(item['description']) 
#'ADDED_KEY' = 'lang' - should be added as a data field to EVERY alert
#'ADDED_VALUE' = 'en' or 'ger' - should be the detected language [via detect()] from data field 'description' of every alert 

with open("kiel.json", 'w') as f:
    f.write(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))

太多了!那对我有用!如果“警报”不作为数据字段包含在数据中,我为该案例添加了我的if问题。。。也许你又有主意了?太多了!太多了!那对我有用!如果“警报”不作为数据字段包含在数据中,我为该案例添加了我的if问题。。。也许你又有主意了?太多了!