python3 execjs 执行js代码, 代码里有中文和转义符就报错

麻烦大神给看下谢谢

jss = """
var apps =[
  {
    "id": "13970",
    "uid": "193586",
    "title": "\u8fd0\u52a8\u6b65\u6570\u5b9d\u6b65\u6b6",
    "price": "1620.00",
    "price_max": "1980.00",
    "purchases": "121",
    "ad_support": "1",
    "wxapp_support": "2",
    "encrypt": "0",
    "createtime": "1533455964",
    "type": "1",
    "displayorder": "1",
    "price_range": "1620.00 - 1980.00",
    "is_new": true,
    "date_format": "2018-08-05",
    "month_day_format": "08.05",
    "time_format": "15:59:24",
    "username": "hudasd"
    }
];
let getDB=()=>apps;
"""
compile = js_engine.compile(jss)
res = compile.call('getDB')
print(res)
exit()


UnicodeEncodeError: 'ascii' codec can't encode characters in position 132-143: ordinal not in range(128)
阅读 6.9k
2 个回答

后面路过的换上 js2py,可以解决中文bug

import js2py
list = js2py.eval_js(jsstr)

可以试下这样

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

如果不行,看下你的字符编码方式是什么

>>>import sys
>>>sys.stdout.encoding
'UTF-8'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题