json.dumps和UnicodeDecodeError

时间:2013-06-28 20:49:13

标签: python json encoding jvectormap

我正在努力制造"转换器" jvectormap中的脚本正在工作但我收到以下错误,

ultural$ python ../../../../jvectormap/converter/converter.py --width 1000 --country_name_index 1 --country_code_index 30 --where "'ISO = DEU'" --projection aea --name datag_germany --language en_US ne_10m_admin_1_states_provinces_shp.shp here.js
ERROR 1: Not enough tokens to complete expression.
Traceback (most recent call last):
  File "../../../../jvectormap/converter/converter.py", line 294, in <module>
    converter.convert(args['output_file'])
  File "../../../../jvectormap/converter/converter.py", line 182, in convert
    open(outputFile, 'w').write( self.map.getJSCode() )
  File "../../../../jvectormap/converter/converter.py", line 30, in getJSCode
    return "jQuery.fn.vectorMap('addMap', '"+self.name+"_"+self.projection['type']+"_"+self.language+"',"+json.dumps(map)+');'
  File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
    chunks = list(self.iterencode(o))
  File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File "/usr/lib/python2.6/json/encoder.py", line 275, in _iterencode_dict
    for chunk in self._iterencode(value, markers):
  File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File "/usr/lib/python2.6/json/encoder.py", line 273, in _iterencode_dict
    yield encoder(key)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 4: unexpected end of data

我想知道,我应该如何处理json.dumps和编码 - 这似乎是本案中的主要问题。我正在使用python 2.6;

1 个答案:

答案 0 :(得分:2)

我通过将包含“异国”字符的字符串转换为utf8解决了json转储和编码的类似问题,从而处理了unicode对象。 这对我有用:.decode('utf8')

这应该有所帮助: http://www.joelonsoftware.com/articles/Unicode.html