嗨,我是一个新的编码,但想学习我写了这个cod,让用户插入一个输入,并应稍后将其保存为文本文件(.txt)与json,然后我想打开文件为(.txt)文件,以便以后能够修改所需的信息
保存用户信息时,文件内容如下所示:
{"Titel": "kalle"}{"Titel": "peter"}{"Titel": "kim"}{"Titel": "sena"}
但当我想用突击队打开文件时:
import json
with open('testtitel.txt') as f:
json_data = json.load(f)
print(json_data)
此文本将显示:
Traceback (most recent call last):
File "openjson.py", line 4, in <module>
json_data = json.load(f)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 19 (char 18)
有什么错,如果有人可以显示什么需要改变,并解释为我是新的编码我会添加代码以及
enter code
import json
class infoCd():
def __init__(self, EnTitel):
self.EnTitel = EnTitel
# //load to json dic function sa simon
def make_dict(self):
the_dict = {"Titel":self.EnTitel}
return the_dict
def readFromDict(self, inputDict):
self.EnTitel = inputDict["Titel"]
def display_cd_details(self):
print(" Title: {} ".format(self.EnTitel))
def see_details(self):
with open('testtitel.txt') as json_file:
testtitel = json.load(json_file)
for p in testtitel:
print(p)
def save_cd_details(cd):
with open('testtitel.txt', 'a') as txtfile:
json.dump(cd.make_dict(), txtfile)
def insert_cd_details():
counter = 0
lista = []
while(True):
EnTitel = input("Insert a titel: ")
counter+=1
Cd = infoCd(EnTitel )
lista.append(Cd)
print(lista)
save_cd_details(Cd)
avslutaCd = input(" Do you still want to(j/n)")
if (avslutaCd == "j"):
print("JAAA")
else:
print("Nej")
break
return lista
a = insert_cd_details()
for cd in a:
cd
print(a)
// code to open
with open('testtitel.txt') as f:
json_data = json.load(f)
print(json_data)
这里
您尝试解码的字符串不是有效的JSON。我想你想要这个:
相关问题 更多 >
编程相关推荐