ITeye博客

python 实用技巧

teder · 2011-06-20 13:39 · Python · 编程语言 · Markdown

  • 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[0],r[1]])

reader = csv.reader(file('brokerresource.csv', 'rb'))
for line in reader:
       if None==line or line[0]==None:
                   continue
       else:
                   print line[0]

 

  • file操作
f = open("building_suggest.txt","w+")
 f.write(str(r[1]))
 f.write("\n")

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。