Shell或Python脚本来更改json fi的值

2024-06-16 10:49:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Json文件控制器.txt我日常工作需要的

{
    "user": "dexter",
    "Issue": "**MUX-4190**",
    "start_date": "**2018-01-01**",
    "end_date": "**2018-12-30**",
    "Demo_nos": [**111**],
    "service_names": [
             "**Demo1.service.test**",
             "**Demo2.service.test**", 
             "**Demo3.service.test**",
             "**Demo4.service.test**",
             "**Demo5.service.tes**t"
        ]
}

所以我总是需要更改json文件中下面突出显示的条目, 有没有什么方法(使用shell/python)可以输入这些值,并且这些字段会相应地填充,而不是手动编辑控件.txt每次都是json文件???你知道吗

  • 发行
  • 开始日期
  • 结束日期
  • 演示编号
  • 服务名称

Tags: 文件testtxtjsondatedemoserviceissue
1条回答
网友
1楼 · 发布于 2024-06-16 10:49:24
import json

with open('your_file.json', 'r+') as f:
    data = json.load(f)
    data['Issue'] = 'Example' # Update your value
    data['start date'] = 'Your date'
    json.dump(data, f, indent=4)
    f.truncate()

相关问题 更多 >