191145692 发表于 2017-4-22 10:08:07

python 实用技巧


[*]python 连接GBK mysql数据库

#!/usr/bin/python
#coding:utf-8
import MySQLdb
fconn = MySQLdb.connect(host='freeshop.dbm.hz1.koubei.com',user='freedian',passwd='8129927@freedian',db='freedian',charset='utf8')
fcursor = fconn.cursor()
......
fcurosr.close()
fconn.close()


[*]异常捕获

try:
...
except Exception,e:
...
 

[*]中文转换

s.encode('gbk')
 

[*]excel操作

writer=csv.writer(file('building.csv','wb'))
writer.writerow(,r])
reader = csv.reader(file('brokerresource.csv', 'rb'))
for line in reader:
if None==line or line==None:
continue
else:
print line
 

[*]file操作

f = open("building_suggest.txt","w+")
f.write(str(r))
f.write("\n")
页: [1]
查看完整版本: python 实用技巧