Python json.dumps不';t使用unicode字符串字典
我有一份清单:Python json.dumps不';t使用unicode字符串字典,python,json,flask,Python,Json,Flask,我有一份清单: [{'probabilities': {'wrong': 0.99991131, 'correct': 8.873138e-05}, 'sentence': u'hello world'}, {'probabilities': {'wrong': 0.99996436, 'correct': 3.5610035e-05}, 'sentence': u'London is the capital of Great Britain'}] dumps不会将此列表转换为字符串,甚至不
[{'probabilities': {'wrong': 0.99991131, 'correct': 8.873138e-05}, 'sentence': u'hello world'}, {'probabilities': {'wrong': 0.99996436, 'correct': 3.5610035e-05}, 'sentence': u'London is the capital of Great Britain'}]
dumps不会将此列表转换为字符串,甚至不会引发任何异常
有完整的代码:
import json
from flask import Flask
from flask import request
from network import predict
app = Flask(__name__)
@app.route("/", methods=['POST'])
def main():
res = []
for obj in request.json:
res.append({'sentence': obj['sentence'], 'probabilities': predict(obj['sentence'])})
print('res', res)
try:
print('json.dumps(res)', json.dumps(res, ensure_ascii=False))
except Exception:
print('Exception', Exception)
return json.dumps(res)
if __name__ == "__main__":
app.run()
那会发生什么?所有烧瓶的东西都是a所需要的吗?我想这不是关于
json.dumps
,而是你的predict函数。我在没有预测功能的情况下运行了它。它可以使用或不使用unicode字符串。你能确认预测功能工作正常吗?我已经解决了这个问题。问题在于预测函数。它返回了一些数字类型,json.dumps不支持这种类型。