Tips
1.%r 和%s
%r在print '%r' %("中文时")会输出一些non-ASCII characters;而%s就还会直接输出中文.
2.输入
raw_input()
3.python编程规范推荐
a = [1,2,3,4]
for i, item in enumerate(a):
print i, item
# 打印
# 0 3
# 1 4
# 2 5
4.pyMongo中的
cursor.find(sort=[('filed_name','sort_type')])需要动态改变
5.python的json.dumps()和json.dump()的区别
1.dumps是将dict转换成str格式,loads是将str转换成dict格式。
import json
a = {'name':'JSDu','age':22}
b = json.dumps(a)
print b, type(b)
{"age":22,"name":"JSDu"}<type 'str'>
json.loads(b)
{u'age':22,u'name':u'JSDu'}
print type(json.loads(b))
<tyoe 'dict'>
2.接着看dump和dumps的区别
import json
a = {'name':'JSDu','age':22}
b = json.dumps(a)
print b, type(b)
{"age":22,"name":"JSDu"}<type 'str'>
c = json.dump(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-92dc0d929363> in <module>()
----> 1 c = json.dump(a)
TypeError: dump() takes at least 2 arguments (1 given)
通过iPython的help()函数查询json.dump
import json
a = {'name':'JSDu'}
fp = file('tesr.txt', 'w')
type(fp)
file
json.dump(a,fp)
fp.close()
cat test.txt
{'name':'JSDu'}
网友评论