json.decoder.JSONDecodeError: Expecting ',' 分隔符: 第 1 行第 11 列(字符 10)



我正在为Stardew Valley制作一个袖珍指南应用程序,作为我的第一个项目。一切都很顺利,直到我添加了代码读取database.txt文件信息的部分。我不知道我能尝试什么。错误如下:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersdanibAppDataLocalProgramsPythonPython38-32libtkinter__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/danib/PycharmProjects/StardewApp/Main.py", line 42, in <lambda>
searchBarEntry.bind("<Return>", lambda event: (search(searchBarEntry.get()), searchBarEntry.delete(0, "end")))
File "C:/Users/danib/PycharmProjects/StardewApp/Main.py", line 13, in search
dict_data = json.loads("{" + the_data + "}")
File "C:UsersdanibAppDataLocalProgramsPythonPython38-32libjson__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:UsersdanibAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersdanibAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 11 (char 10)
Process finished with exit code 0

这是我的代码:

import tkinter as tk
import json

def search(entry):
print("This is the entry: " + entry)
entry = entry.lower()
entry = entry.replace(" ", "")
data_read = open("database.txt", "r")
the_data = data_read.read()
dict_data = json.loads("{" + the_data + "}")
data_read.close()
try:
infoLabel[dict_data[entry]]
except KeyError:
data_append = open("database.txt", "a")
infoLabel["text"] = "No data, please give some."
item_info = input()
data_append.write(", n"" + entry + "": " + """"" + item_info + """"")
data_append.close()

root = tk.Tk()
root.geometry("720x480")
bgImg = tk.PhotoImage(file="bg.png")
bgLabel = tk.Label(root, image=bgImg)
bgLabel.place(relwidth=1, relheight=1)
bt1Img = tk.PhotoImage(file="icon.png")
defBtImg = tk.PhotoImage(file="define.png")
searchBarFrame = tk.Frame(root, bd=5, bg="gray50")
searchBarFrame.place(relwidth=0.92, relheight=0.1,
relx=0.04, rely=0.08)
searchBarEntry = tk.Entry(searchBarFrame, font=50, bd=0,
bg="gray55", fg="gray15")
searchBarEntry.bind("<Return>", lambda event: (search(searchBarEntry.get()), searchBarEntry.delete(0, "end")))
searchBarEntry.place(relwidth=1, relheight=1)
infoFrame = tk.Frame(root, bd=5, bg="gray50")
infoFrame.place(relwidth=0.7, relheight=0.72,
relx=0.04, rely=0.20)
infoLabel = tk.Label(infoFrame, bg="gray55", justify="left", font=("courier", "12"), anchor="nw")
infoLabel.place(relwidth=1, relheight=1)
sideFrame = tk.Frame(root, bd=5, bg="gray50")
sideFrame.place(relwidth=0.2, relheight=0.72,
relx=0.76, rely=0.20)
sideButton1 = tk.Button(sideFrame, bg="gray55", bd=0, image=bt1Img)
sideButton1.place(relwidth=1, relheight=0.2)
root.mainloop()

这也是包含信息的文本文件。

"axe": """Axe
The Axe is the axe you start with. Nothing special.
How many chops to break:
Tree        - 10
Stump       - 5
Large Stump - Incapable
Large Log   - Incapable"""

不能在json中使用多行字符串。使用\n和\t格式化数据。如果它的json数据,为什么保存为txt和pre-postfix{}?

JSON 中的多行字符串

这样做:


{
"axe": "AxennThe Axe is the axe you start with. Nothing special.nnHow many chops to break:nTree        - 10nStump       - 5nLarge Stump - IncapablenLarge Log   - Incapable"
}

或者,如果你真的需要它在你的.txt文件中,那么只需添加到你的txt文件中:


"axe": "AxennThe Axe is the axe you start with. Nothing special.nnHow many chops to break:nTree        - 10nStump       - 5nLarge Stump - IncapablenLarge Log   - Incapable"

相关内容

最新更新