python - 如何在 json 中添加数据字段(键值)并针对单个数据字段进行语言检测
问题描述
我有天气警报数据,例如
"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
},
现在我想向每个警报字典添加一个键值对,其中包含从“描述”字段中检测到的语言。我试过了,但无法获得正确的语法......
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 和 for 循环?再次感谢!
解决方案
您只需要遍历字典键alerts
并将键、值添加到每个item
(这是一个字典)。
for item in data["alerts"]:
item["ADDED_KEY"] = "ADDED_VALUE"
推荐阅读
- javascript - 有没有办法让选择的内容向下推送?
- python - 使用 RandomizedSearchCV 时多个作业的问题
- groovy - 是否习惯将 Groovy 变量初始化为默认值?
- python - 寻找重塑数据框的有效方法:从 n*m 到 1*(n+m)
- amazon-web-services - 负载均衡的 Elastic Beanstalk 中来自 AWS EC2 的出站请求的 IP 地址范围
- reactjs - React-上下文提供者使用与消费者相同的值重新呈现
- c# - ASP.NET CORE DataFormat 正在影响编辑操作
- javascript - 如何强制输入 html 元素仅显示最后输入的字符
- java - 对摩尔斯电码翻译器使用递归时的堆栈溢出
- vim - 如何使 vim 设置特定于文件扩展名