简体   繁体   English

在 python Centos8 中实现 json

[英]Implement json in python Centos8

I have a.py script to modify a json file.我有一个 .py 脚本来修改 json 文件。 However what I'm doing is loading a json file and then modify it with my code.但是我正在做的是加载 json 文件,然后用我的代码修改它。 What I have is the next ñ:我所拥有的是下一个ñ:


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

I'm trying something like this but I got the next error in CentOs8我正在尝试这样的事情,但我在 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

Is there any solution to automatize the json file?是否有任何解决方案可以自动化 json 文件? So when I'm using the command python3 main.py example.py helloWorld.json to generate a modify.json from that helloWorld.json file因此,当我使用命令python3 main.py example.py helloWorld.json从 helloWorld.json 文件中生成 modify.json

I'm using Centos8 and the path of the files are /root我正在使用 Centos8,文件的路径是 /root

Thanks in advance!提前致谢!

your str(sys.argv[1:]) is "['helloWorld.json']" which is not file.你的str(sys.argv[1:])"['helloWorld.json']"这不是文件。 try using:尝试使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM