使用plone对象创建python json
我有一个包含plone对象的使用plone对象创建python json,plone,Plone,我有一个包含plone对象的json文件,其中有一个对象字段给了我一个错误: UnicodeDecodeError('ascii', '{"id":"aluminio-prata", "nome":"ALUM\xc3\x8dNIO PRATA", "num_demaos":0, "rendimento": 0.0, "unidade":"litros", "url":"", "particular":[], "profissional":[], "unidades":[]},', 36, 37,
json
文件,其中有一个对象字段给了我一个错误:
UnicodeDecodeError('ascii', '{"id":"aluminio-prata", "nome":"ALUM\xc3\x8dNIO PRATA", "num_demaos":0, "rendimento": 0.0, "unidade":"litros", "url":"", "particular":[], "profissional":[], "unidades":[]},', 36, 37, 'ordinal not in range(128)') (Also, the following error occurred while attempting to render the standard error message, please see the event log for full details: 'NoneType' object has no attribute 'getMethodAliases')
我已经知道witch字段是,是title=obj.pretty\u title\u或id()中的“title”
,当我从这里删除它时,它是ok的:
json += '{"id":"' + str(id) + '", "nome":"' + title + '", "num_demaos":' + str(num_demaos) + ', "rendimento": ' + str(rendimento) + ', "unidade":"' + str(unidade) + '", "url":"' + link_produto + '", "particular":' + arr_area_particular + ', "profissional":' + arr_area_profissional + ', "unidades":' + json_qtd + '},
但当我离开它时,我犯了这个错误
UnicodeDecodeError('ascii', '{"id":"aluminio-prata", "nome":"ALUM\xc3\x8dNIO PRATA", "num_demaos":0, "rendimento": 0.0, "unidade":"litros", "url":"", "particular":[], "profissional":[], "unidades":[]},', 36, 37, 'ordinal not in range(128)') (Also, the following error occurred while attempting to render the standard error message, please see the event log for full details: 'NoneType' object has no attribute 'getMethodAliases')
我假设在读取JSON文件时会发生错误 在内部,Plone几乎所有内容都使用Python Unicode字符串。如果从文件中读取字符串,则需要先将其解码为Unicode,然后Plone才能使用它。如果您没有给出其他说明,Python将假定字符串编码为ASCII,并将在此基础上尝试Unicode转换。这类似于写作:
unicode("ALUM\xc3\x8dNIO PRATA")
这将产生同样的错误
事实上,您使用的字符串显然是用UTF-8字符集编码的。从“\xc3”中可以明显看出这一点,这也是有意义的,因为这是Plone向外部世界发送数据时使用的字符集
那么,你如何解决这个问题?必须指定转换为Unicode时要使用的字符集:
"ALUM\xc3\x8dNIO PRATA".decode('UTF8')
这将为您提供一个没有错误的Python Unicode字符串
因此,在将JSON文件读入字符串(我们称之为
mystring
)后,需要使用mystring.decode('UTF8')
对其进行显式解码unicode(mystring,'UTF8')
是同一操作的另一种形式。正如Steve已经写的那样做title.decode('UTF8')
一个例子说明了以下事实:
>>> u"Ä" == u"\xc4"
True # the native unicode char and escaped versions are the same
>>> "Ä" == u"\xc4"
False # the native unicode char is '\xc3\x84' in latin1
>>> "Ä".decode('utf8') == u"\xc4"
True # one can decode the string to get unicode
>>> "Ä" == "\xc4"
False # the native character and the escaped string are
# of course not equal ('\xc3\x84' != '\xc4').
我发现这对解决UTF-8编码/解码的问题和理解非常有帮助。如果我们不知道您是如何生成此CSV的,并且没有共享源,我们如何帮助您?你查过了吗?