【问题标题】:Implement json in python Centos8在python Centos8中实现json
【发布时间】:2021-03-25 02:48:16
【问题描述】:

我有一个 .py 脚本来修改 json 文件。但是我正在做的是加载一个 json 文件,然后用我的代码修改它。我所拥有的是下一个 ñ:


 with open('example.json', 'r+') as file:
    dictionary_data = json.load(file)
    .  
    .(code)
    .
    .
    .
 new_file = open("modify.json", "w") 

我正在尝试这样的事情,但我在 CentOs8 中遇到了下一个错误

with open(str(sys.argv[1:]), 'r+') as file:
  dictionary_data = json.load(file)
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    with open(str(sys.argv[1:]), 'r+') as file:
    FileNotFoundError: [Errno 2] No such file or directory: "['helloWorld.json']"
The HelloWorld file is in the same directory as the main.py

有没有办法自动化 json 文件?因此,当我使用命令 python3 main.py example.py helloWorld.json 从该 helloWorld.json 文件生成 modify.json 时

我用的是Centos8,文件的路径是/root

提前致谢!

【问题讨论】:

    标签: python json centos8


    【解决方案1】:

    您的str(sys.argv[1:])"['helloWorld.json']",这不是文件。 尝试使用:

    with open(str(sys.argv[1]), 'r+') as file: # for example.json
    
    with open(str(sys.argv[2]), 'r+') as file: # for helloworld.json
    

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 2020-03-30
      • 2011-05-06
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 2022-01-10
      • 2016-01-01
      相关资源
      最近更新 更多