【问题标题】:How to change the JSON file contents for the below json file如何更改以下 json 文件的 JSON 文件内容
【发布时间】:2019-03-25 10:07:47
【问题描述】:

我有一个 json 文件,比如 1.control_file.txt,我需要不时更改它的值,所以说我有一张票 Sample1,我需要更改日期字段,有时还需要其他字段. 因此,假设说我获取了票号作为用户输入,那么我如何更改票证字段以及假设给定的开始和结束日期。 json文件中的出口商名称标签也应该是可变的。 任何人都可以建议我如何使用 shell 或 python 做到这一点?

我作为用户输入的字段 user、ticket、startdate、end_date 和 sample_names...

 "user": "dexter",
 "ticket": "Sample1",
 "start_date": "2018-07-02",
 "end_date": "2019-07-02",
 "sample_names": [
    "Demo1exp1",
    "Demo2exp2",
    "Demo3exp3",
    "Demo4exp4",
    "Demo5exp5",
    "Demo6exp6",
    "Demo7exp7",
    "Demo8exp8",
    "Demo9exp9"
 ]
}```

【问题讨论】:

    标签: python json shell


    【解决方案1】:

    这个sn-p的代码可能就是你需要的

    import json
    
    with open('data.txt') as json_file:  
        data = json.load(json_file)
    
    data['start_date'] = "2018-07-03" 
    
    with open('data.txt', 'w') as outfile:  
        json.dump(data, outfile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2017-03-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2020-04-06
      • 2020-05-17
      相关资源
      最近更新 更多