简体   繁体   中英

json.dumps and UnicodeDecodeError

I am trying to make the "converter" script in jvectormap into work but I am getting the following error,

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

I was wondering, how should I deal with json.dumps and the encoding - which seems is the major issue in this case. I am using python 2.6;

I resolved a simmilar issue with json dumps and encoding by converting strings containing "exotic" characters to utf8, thus handling unicode objects.. This worked for me: .decode('utf8')

This should help: http://www.joelonsoftware.com/articles/Unicode.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM