gpt4 book ai didi

python - 为什么使用使用 utf-8 编码的 Windows txt 文件时会出现 Json 的 ValueError 错误?

转载 作者:行者123 更新时间:2023-12-01 04:27:56 24 4
gpt4 key购买 nike

方法如下:

def load(self, path):
json_data = open(path, "r")
self.data = load(json_data)
return self.data

这是文件(使用 utf-8 编码保存):

{
"False": "Falso",
"None": "Nulo",
"True": "Verdadeiro",
"as": "como",
"assert": "afirmar",
"break": ["quebrar", "interromper", "parar"],
"class": "classe",
"continue": "continuar",
"def": ["func", " f ", "função"],
"del": ["deletar", "excluir"],
"elif": ["senão se", "senao se"],
"else": ["senão", "senao"],
"except": ["exceto", "excetuar"],
"finally": "finalmente",
"for ": "para ",
"from ": "de ",
"if ": ["se ", "caso "],
"if(": ["se(", "caso("],
"import": "importar",
" in ": [" em ", " no ", " na ", " nos ", " nas "],
" is ": " é ",
"nonlocal": "nãolocal",
" not ": " não ",
" or ": " ou ",
"pass": ["passar", "prosseguir"],
"raise": ["levantar", "erguer"],
"return": "retornar",
"try": "tentar",
"while": "enquanto",
"with": "com",
"print": ["escrever", "imprimir", "printar", "mostrar"],
" and ": " e "
}

这是错误:

ValueError: Expecting value: line 1 column 1 (char 0)

如果我将其编码为“ANSI”,此错误就会消失。我不明白,字符 0 是 {.

最佳答案

BOM 一定是罪魁祸首。使用 codecs.open(path,"r","utf-8-sig") 打开文件或自动检测要打开的编码,例如根据Reading Unicode file data with BOM chars in Python .

关于python - 为什么使用使用 utf-8 编码的 Windows txt 文件时会出现 Json 的 ValueError 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com