python - 如何对大型 JSON 文件进行基于数学的批量更改?
问题描述
假设我有几个名称的目录,如下所示:
{
"name": "Miller John",
"mobile": "897654321",
"age": 45,
"salary": 120000,
"address": {
"city": "New York",
"country": "USA"
}
}
我现在想将每个人的工资字段除以k并将当前工资替换为该工资除以k。对于非常大的 JSON,我该怎么做?
编辑:
我正在处理的实际代码是一个包含多个项目的 NDJSON,其中第一个项目如下:
{"index":{"_id":1}}
{"name":"Wine - Maipo Valle Cabernet","price":152,"in_stock":38,"sold":47,"tags":["Alcohol","Wine"],"description":"Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem. Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat. Praesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede. Morbi porttitor lorem id ligula.","is_active":true,"created":"2004\/05\/13"}
我正在尝试将价格字段除以 10。
解决方案
您可以jq
像这样在终端中使用:
jq '.salary= (.salary/1000)' YourFile.json
输出
{
"name": "Miller John",
"mobile": "897654321",
"age": 45,
"salary": 120,
"address": {
"city": "New York",
"country": "USA"
}
}
推荐阅读
- javascript - 在angularjs中突出显示从一个列表框移动到另一个列表框的项目
- c# - 合并两个 XDocument
- android - 数据源PagedList Android无效
- kubernetes-helm - <.Chart.Name>:无法评估字符串类型中的字段图表
- ssh - 是否可以告诉 ansible 不要使用 ~/.ssh/config?
- python - 如何在压缩工具中获取下拉值的文本?
- docker - 如何找到从 SHA1 中提取的图像?
- r - 根据条件过滤id
- c# - 方法中的程序集名称返回继承的类名
- maven - 如何使用dependencyManagement删除spring boot依赖?