问题:用putty连接虚拟机,运行pygame程序后,无法显示,直接乱码,创建窗口失败
方法:原因是没有X Server,可以在windows下安装Xming程序,同时更改putty的连接设置,之后再运行你开发的pygame程序就可以了。
同时不安装Xming的话,python GUI库tkinter也无法使用,会报错,如下:
_tkinter.TclError: couldn't connect to display "localhost:10.0"
注:配置信息参考http://www.2cto.com/os/201407/315646.html
分类 "Python" 下的文章
python的json.dumps方法默认会输出成这种格式"u535au5ba2u56ed",
要输出中文需要指定ensure_ascii参数为False,如下代码片段:
json.dumps({'text':"中文"},ensure_ascii=False,indent=2)
这里的indent是让json格式有换行空两格显示
{% if 1<current<total %}
{{total_page}}页
{% elif (current==1 and current<total) %}
{{cur_page}}/{{total_page}}
{% else %}
末页
{% end %}
{{ {0:u"未读",1:u"已读",2:"回复"}[m[2]] }}
错误情况:UnicodeEncodeError: 'ascii' codec can't encode characters in position······
指定一个字条串为unicode编码
str = u'你好,hello'
判断字条串是否为unicode
isinstance(str, unicode)
字符串str为unicode码时
str.encode('utf-8') #这样就可以转换成utf-8编码了
要进行编码转换,都需要先用decode方法将其转换成unicode编码,再使用encode方法将其转换成其他编码。
注:http://www.jb51.net/article/17560.htm
注:这是python2.7,在python3中不适用
ur'你好,中国!
' 输出:‘你好,中国!
’
这表示字符串是一个普通字符,是unicode码
r是为了防止字符串被转义,没有r后面的
就会被转义为换行
u是说明这个字符串是unicode码,防止出现乱码情况