除去`在Python输出u`字符 [英] removing `u` character in python output

查看:98
本文介绍了除去`在Python输出u`字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个表单,当我点击提交按钮,我分配3值转换为JavaScript字典,并将其发送到一个python脚本不过来处理我的web浏览器告诉我一个错误!

从Json的错误:{u'food':90,u'cargo':70,u'fuel':50}的SyntaxError

controller.js

 函数customiseCtrl($ XHR){
VAR自我=这一点;CHECKPOINT();
this.process =功能(){
    如果(检查点()){        VAR newPlayer = {燃料:价值,食:值2,货物:值3};        $ XHR('POST','/进程的,newPlayer,功能(code,响应){
            self.x =响应;        });
    }
};
}

/过程 - > python脚本(我想读的信息的信息,并将其写入到谷歌应用程序引擎

  DEF后(个体经营):
 用户= users.get_current_user()
 玩家=播放器(); 信息= json.loads(self.request.body)
 player.fuel = info.fuel
 self.response.out.write(信息)


解决方案

打印Python字典在许多情况下无法产生有效的JSON。您希望 JSON 模块:

 进口JSON#...略...self.response.out.write(json.dumps(信息))
# 要么
那样json.dump(资讯,self.response.out)

I create a form and when I click the submit button, I assign the 3 value into a javascript dict and send it over to a python script to process however My web browser tell me a error!

from Json error: {u'food': 90, u'cargo': 70, u'fuel': 50} SyntaxError

controller.js

function customiseCtrl($xhr){
var self = this;

checkPoint();
this.process = function(){
    if (checkPoint()){

        var newPlayer = {"fuel":value, "food":value2, "cargo":value3 };

        $xhr('POST', '/process', newPlayer, function (code, response) {
            self.x = response;

        });
    }
};


}

/process --> python script (I am trying to read the information of "info" and write it into the Google app engine.

def post(self):
 user = users.get_current_user()
 player = Player();

 info = json.loads(self.request.body)
 player.fuel = info.fuel
 self.response.out.write(info)

解决方案

Printing a Python dict will in many cases not generate valid JSON. You want the json module:

import json

# ... snip ...

self.response.out.write(json.dumps(info))
# or
json.dump(info, self.response.out)

这篇关于除去`在Python输出u`字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆