如何在Python中使json文件成为列表
如前所述,我想打开一个json文件并将其放入一个列表中,以便向其添加新元素,然后将所有内容转储回json文件 这是我的代码(注释部分是我以前尝试过的):如何在Python中使json文件成为列表,python,json,Python,Json,如前所述,我想打开一个json文件并将其放入一个列表中,以便向其添加新元素,然后将所有内容转储回json文件 这是我的代码(注释部分是我以前尝试过的): 导入json #从文件中读取 打开(“demofile.txt”、“r”)作为f:x=f.read() #解析 y=json.loads(x) #编辑 y[“user”]={“fname”:“John”,“lname”:“Who”} #保存到文件 打开(“demofile.txt”、“w”)作为f:f.write(json.dumps(y))
导入json
#从文件中读取
打开(“demofile.txt”、“r”)作为f:x=f.read()
#解析
y=json.loads(x)
#编辑
y[“user”]={“fname”:“John”,“lname”:“Who”}
#保存到文件
打开(“demofile.txt”、“w”)作为f:f.write(json.dumps(y))
要从文件中读取
JSON
:
import json
with open('data.txt') as json_file:
data = json.load(json_file)
要添加新数据,请执行以下操作:
data['key'] = "value"
要将JSON
写入文件:
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
如果您从JSON文件、JSON列表中读取,然后编写自定义字符串,那么它将不起作用。因为下次读取JSON文件时,它会失败 因此,在写入/保存过程中,应该将其设置为json本身。下面的代码解释了如何做
import json
class Carta:
def __init__(self, filename):
self.__filename = filename
self.__lista = list()
self.read_from_json_file()
def read_from_json_file(self):
with open(self.__filename) as file:
self.__lista = json.load(file)
def write_to_json_file(self):
with open(self.__filename, 'w') as f:
json.dump(self.__lista, f)
def add(self, value):
self.__lista.append(value)
您应该将打开(文件名,模式)的用作f:
而不是f=open(文件名)
的原因是,在with块的末尾,文件会自动关闭。否则,每次打开文件时都必须调用f.close()
json.load
-从文件中读取json数据,转换为python数据类型/结构
json.dump
-读取python数据类型/结构,将其转换为字符串并存储在文件(文件句柄)中,然后保存文件
使用pdb跟踪错误
import json
import pdb
class Carta:
def __init__(self, filename):
self.__filename = filename
self.__lista = list()
self.read_from_json_file()
def read_from_json_file(self):
pdb.set_trace() # to pause execution and start debugger
# When paused,
# type n to continue to next line,
# type c to continue execution or to continue to the next loop
# type b <file_name>:<line_number> to add another break point, where <file_name> and <line_number> are place holders
# Example, b /home/username/hello.py:43, will add breakpoint at 43 line of hello.py in /home/username path
# type q to quit debugger and halt execution
with open(self.__filename) as file:
self.__lista = json.load(file)
def write_to_json_file(self):
with open(self.__filename, 'w') as f:
json.dump(self.__lista, f)
def add(self, value):
# Second breakpoint
pdb.set_trace()
self.__lista.append(value)
导入json
导入pdb
等级证书:
def uuu init uuu(self,文件名):
self.\uu filename=文件名
self.\uu lista=list()
self.read_from_json_file()
def从_json_文件读取(self):
pdb.set_trace()#暂停执行并启动调试器
#暂停时,
#键入n继续下一行,
#键入c继续执行或继续下一个循环
#类型b:添加另一个断点,其中和是占位符
#例如,b/home/username/hello.py:43将在/home/username路径中hello.py的43行添加断点
#键入q退出调试器并停止执行
打开(self.\u文件名)作为文件:
self.\uuu lista=json.load(文件)
def write_to_json_文件(self):
将open(self.\u文件名“w”)作为f:
json.dump(self.\u lista,f)
def添加(自身、价值):
#第二断点
pdb.set_trace()
self.\u lista.append(值)
或者直接用
python-mpdb file.py
然后添加断点。它将在第一行本身暂停,并返回一个(pdb)控制台,您可以在其中添加断点。因此我必须在“数据”中添加一个新元素?JSON是一种结构化格式,因此您不能像使用纯文本文件那样简单地将内容添加到文件末尾。列表由一个结束标记来划分,因此您至少必须重写结束以删除旧的结束标记、写入新数据和写入新的结束标记。但是在实践中,如果文件不是非常大的话,我们倾向于只写一个新文件,然后删除旧文件(此时可能会切换到一个合适的数据库,而不是顺序文件)。非常感谢您宝贵的帮助。我对这个网站有点陌生,有没有办法让你更好地了解我不断遇到的错误?如果你有新问题,可以问一个新问题。如果这个答案让你更接近成功,你可能会接受它;或者发布自己的答案并接受。另请参见@MohammedSahib他们是否与这个特定问题相关?@AMC事实上他们是。调用write_to_json_文件时,我得到以下错误:object不是jsonserializable@MohammedSahib你能把你的全部代码添加到OP中吗?或者用它提出一个新问题,我不知道。你能分享这些数据吗?通常,JSON用于类似字典的结构,而不是简单的列表。
import json
import pdb
class Carta:
def __init__(self, filename):
self.__filename = filename
self.__lista = list()
self.read_from_json_file()
def read_from_json_file(self):
pdb.set_trace() # to pause execution and start debugger
# When paused,
# type n to continue to next line,
# type c to continue execution or to continue to the next loop
# type b <file_name>:<line_number> to add another break point, where <file_name> and <line_number> are place holders
# Example, b /home/username/hello.py:43, will add breakpoint at 43 line of hello.py in /home/username path
# type q to quit debugger and halt execution
with open(self.__filename) as file:
self.__lista = json.load(file)
def write_to_json_file(self):
with open(self.__filename, 'w') as f:
json.dump(self.__lista, f)
def add(self, value):
# Second breakpoint
pdb.set_trace()
self.__lista.append(value)