python - Python,将 Json-Data 放入变量中
问题描述
我想将 Json-File 数据放入单个变量中。所以我可以将变量用于其他事情。我能够从其他 QDialog 的输入创建一个 Json 文件。下一步是从 Json 文件中取回输入并将其插入到各个变量中。所以 json:Line1 变成 self.line1Config 等等。
self.jsonfile = str(self.tn+'.json')
def WriteJSON(self):
self.NewConfigJSON = {}
self.NewConfigJSON['configs'] = []
self.NewConfigJSON['configs'].append({
'Line1': str(self.CreatNewJsonW.Line1),
'Line2': str(self.CreatNewJsonW.Line2),
'Line3': str(self.CreatNewJsonW.Line3)
})
jsonString = json.dumps(self.NewConfigJSON)
jsonFile = open(self.jsonfile, 'w')
jsonFile.write(jsonString)
jsonFile.close()
使用下面的代码,它不会工作:
def CheckIfJsonFileIsAlreadyThere(self):
try:
if os.path.isfile(self.jsonfile) == True:
data = json.loads(self.jsonfile)
for daten in data:
self.line1Config = daten['Line1']
self.line2Config = daten['Line2']
self.line3Config = daten['Line3']
else:
raise Exception
except Exception:
self.label_ShowUserText("There are no configuration yet. Please create some
configuartions")
self.label_ShowUserText.setStyleSheet("color: black; font: 12pt \"Arial\";")
错误代码:
Traceback (most recent call last):
File "c:\path", line 90, in CheckIfJsonFileIsAlreadyThere
data = json.loads(self.jsonfile)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 11 (char 10)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\path", line 38, in <module>
MainWindow = mainwindow()
File "path", line 21, in __init__
self.d = Generator_Dialog(self)
File "c:\path", line 55, in __init__
self.CheckIfJsonFileIsAlreadyThere()
File "c:path", line 100, in CheckIfJsonFileIsAlreadyThere
self.label_ShowUserText("There are no configuration yet. Please create some configuartions")
TypeError: 'QLabel' object is not callable
解决方案
由于 JSON 文档来自专门来自 的文件self.jsonfile = str(self.tn+'.json')
,因此请使用json.load()
而不是json.loads()
:
-
将s(包含 JSON 文档的 str、bytes 或 bytearray 实例)反序列化为 Python 对象
-
将 fp(一个
.read()
支持文本文件或包含 JSON 文档的二进制文件)反序列化为 Python 对象
更改此行:
data = json.loads(self.jsonfile)
到
with open(self.jsonfile) as opened_file:
data = json.load(opened_file)
推荐阅读
- string - 如何修复 profanity_filter 字符串转换错误
- background - 如何复制 Squarespace 投资组合条目并保留背景图像?
- kubernetes - 带有需要作业的 initContainer 的水平 Pod 自动缩放 (HPA)
- flutter - 如何在flutter web中接收FCM推送通知数据并将用户重定向到通知中收到的链接?
- python - 当我尝试通过 windows powershell 在 docker 上运行部署 tensorflow 服务器时,收到“--mount”错误消息的“invalid argument”type=bind”
- java - Fastlane Screengrab - 不要在屏幕截图文件名中包含时间戳
- docker - 使用 docker 时出现问题,没有命令有效,“sudo”。怎么解决?
- python - DataLoader worker (pid(s) 5852, 3332, 1108, 5760) 意外退出
- android-studio - 最新更新后,Android Studio 无法在项目导航器中显示 cpp 文件
- express - axios 通过数据传递获取请求,但未正确传递数据