簡體   English   中英

為什么我使用使用utf-8編碼的Windows txt文件在Json中收到ValueError?

[英]Why do I get a ValueError with Json using a windows txt file encoded with utf-8?

方法如下:

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")打開文件,或自動檢測要打開的編碼,例如,按照在Python中使用BOM字符讀取Unicode文件數據的方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM