raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)出现这个问题怎么办?
# 读取JSON数据
def read_json(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
# 假设每个条目占一行
data = [json.loads(line) for line in file if line.strip()]
return data
开始我的读取方法是这样的,然后经过我的修改
# 读取JSON数据
def read_json(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
print(f"文件未找到: {file_path}")
return None
变成这样,报错就消失了