分类 "Python" 下的文章

问题:python中的字典与json有什么区别
解决:就如同整数0与字符‘0’的区别一样,它们格式一样,可以互相转换
方法:
users = {'name':'haha', 'age':12}      此时users为dict
users_json = json.dumps(users)     此时获得的users_json就是一个json格式的str

注:JSON是一种轻量级的数据交换格式,各种语言都有良好的支持。字典是Python的一种数据结构,可以看成关联数组。
如果json转换成dict,json中必须使用双引号,单引号会报错:ValueError: Expecting property name: line 1 column 1 (char 1)

错误:TypeError: datetime.datetime(2015, 12, 16, 7, 19, 34) is not JSON serializable
解决:Python自带的json序列化工具不能序列化datetime类型的数据
方法:
一、将获得的数据列表化
bookList = self.db.query(BookList.id,BookList.name,BookList.published).order_by('published desc').offset(0).limit(10).all()
bookList = [list(b) for b in bookList]
二、对datetime数据格式化
for b in bookLIst:
b[1] = b[1].strftime('%Y-%m-%d %H:%M:%S')
三、完成上面就可以进行json操作了
self.write({'bookLIst':booklist})

问题:用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