在Python中读取旧版本的JSON文件

toiithl6  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(72)

我试图从不同的文件夹读取json文件,但其中的值是该文件的旧版本。然而,同一文件夹中的不同json都能正确读取。如果我将apply.json复制到我的pwd中,则在这种情况下可以正常工作。例如-

'''In the json:
   AmountMax : 0
When read in python:
   AmountMax : 90'''

with open("../frontend/src/components/Presets/apply.json") as infile:
    settings_data = json.load(infile)
    print(settings_data)

字符串
我完全不知道这是如何发生的,任何建议或帮助都是非常感谢的。我已经尝试确保路径是正确的,甚至重新创建了文件。
编辑:这里是输出如下

apply.json -
{
  "Active": "Preset 0",
  "reqAmountMin": 0,
  "reqAmountMax": 0
}

Python Output reading apply.json from orignal folder -
{'reqAmountMin': 0, 'reqAmountMax': 90}

Python Output reading apply.json copied to pwd -
{'Active': 'Preset 0', 'reqAmountMin': 0, 'reqAmountMax': 0}

xiozqbni

xiozqbni1#

检查工作目录

import os
print("Current Working Directory:", os.getcwd())

字符串
如果脚本与JSON文件不在同一目录中(您的情况),则可能需要相应地调整相对路径。

import json

file_path = "/absolute/path/apply.json"

with open(file_path) as infile:
    settings_data = json.load(infile)
    print(settings_data)


将**"/absolute/path/apply.json”**替换为文件的实际绝对路径。

相关问题