简体   繁体   English

为什么我使用使用utf-8编码的Windows txt文件在Json中收到ValueError?

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

Here's the method: 方法如下:

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

Here's the file (Which was saved with utf-8 encoding): 这是文件(使用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 "
}

And here's the error: 这是错误:

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

If I encode it as "ANSI", this error goes away. 如果我将其编码为“ ANSI”,此错误将消失。 I don't understand, char 0 is {. 我不明白,字符0是{。

BOM must be the culprit. BOM必须是罪魁祸首。 Open the file with codecs.open(path,"r","utf-8-sig") or autodetect the encoding to open with eg as per Reading Unicode file data with BOM chars in Python . 使用codecs.open(path,"r","utf-8-sig")打开文件,或自动检测要打开的编码,例如,按照在Python中使用BOM字符读取Unicode文件数据的方式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django中,为什么我对utf-8编码的字符串会遇到问题? - In Django, why do I get problems with utf-8 encoded strings? 如何检测文件是否使用UTF-8编码? - How do I detect if a file is encoded using UTF-8? 如何使用nltk.data.load()从以UTF-8编码的文件中读取CFG? ASCII文件工作正常,但UTF-8编码的文件给出了错误 - How do I read CFG from a file encoded in UTF-8 using nltk.data.load() ? ASCII files works fine but UTF-8 encoded file gives an error json编码为UTF-8字符。 如何在Python请求中作为json处理 - json encoded as UTF-8 characters. How do I process as json in Python Requests Python:将编码的字符串解码为 json 文件中的 utf-8 - Python: decode encoded string to utf-8 inside json file 使用Python 2.6和utf-8的ValueError(“无法解码JSON对象”) - ValueError(“No JSON object could be decoded”) using Python 2.6 and utf-8 如何检查是否已成功在utf-8中进行编码 - How do I check whether have encoded in utf-8 successfully 如何使用 Python 读取 utf-8 编码的文本文件 - How to read a utf-8 encoded text file using Python python-将编码的json转换为utf-8 - python - convert encoded json into utf-8 如何在 python(从 utf-8 编码的文本文件导入)中将组合变音符号 ɔ̃、ɛ̃ 和 ɑ̃ 的字符与非重音字符进行比较? - How do I compare characters with combining diacritic marks ɔ̃, ɛ̃ and ɑ̃ to unaccented ones in python (imported from a utf-8 encoded text file)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM