使用JSON时的Unicode错误

1 投票
2 回答
2371 浏览
提问于 2025-04-17 12:25

我知道之前有类似的问题被提过,但我觉得我遇到的问题和那些稍微有点不同。请耐心听我说;我才开始用Python四个月,肯定还有很多不成熟的地方!

我正在写一个程序,用来显示从CSV文件中获取的LinkedIn数据,使用的是Protovis插件,结果以树状图的形式展示。就我所知,这个插件的设置是正确的,这一切都是基于O'Reilly的《Mining the Social Web》这本书。不过,当我在IDLE中运行我的代码时,出现了以下错误信息:

Traceback (most recent call last):
 File "C:/Users/Envy 15/Desktop/MASIDendo", line 115, in <module>
    html = open(HTML_TEMPLATE).read() % (json.dumps(json_output),)
  File "C:\Python27\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 17: invalid start `byte`

根据我的理解,出现Unicode错误的原因是我的某个文件名中有非Unicode字符,但我检查过了,情况并不是这样。错误信息指向我代码中的这一部分:

html = open(HTML_TEMPLATE).read() % (json.dumps(json_output),)
f = open(os.path.join(os.getcwd(), 'out', OUT), 'w')
f.write(html)
f.close()

print 'Data file written to: %s' % f.name

# Open up the web page in your browser

webbrowser.open('file://' + f.name)

如果有人能帮我解决这个问题,我将非常感激!

2 个回答

0

听起来你的 json_output 对象里面有一个字符串,它不是unicode格式,或者说不能被转换成unicode格式。问题不在于文件名。

3

检查一下你的基础,验证一下json_data的内容,可以使用repr()或者pprint.pprint()来帮助你。

字符串和unicode对象有encode和decode这两个方法,它们可以接受错误处理的参数,比如这样使用:"\x66\x89".decode("utf-8", "replace")

json.dumps是把数据转换成json格式的,奇怪的是你把json_output作为输入传给它。

撰写回答