python 从txt导数据到mysql

!/usr/bin/python

encoding:utf-8

import MySQLdb
db = MySQLdb.connect("127.0.0.1", "zhanghao", "mima", "shujuku", charset='utf8' )
cursor = db.cursor()
i=0
with open("d.txt") as f:
for line in f.readlines():
a=line.split()
i=i+1
j=0
for aa in a:
j=j+1
if j == 4:
x=aa
elif j == 5:
elif j == 7:
y=aa
elif j == 8:
y=y+" "+aa
kssj=y
elif j == 9:
z=aa
elif j == 10:
z=z+" "+aa
jssj=z
elif j == 1:
type=aa
elif j == 2:
wtms=aa
elif j == 3:
wtly=aa
elif j == 6:
clr=aa
elif j == 11:
clbf=aa
try:
if type.decode('utf-8') == '日常巡检'.decode('utf-8'):
tt=0
elif type.decode('utf-8') == '网络设备配置'.decode('utf-8'):
tt=1
elif type.decode('utf-8') == '安全设备配置'.decode('utf-8'):
tt=2
elif type.decode('utf-8') == '系统部署'.decode('utf-8'):
tt=3
elif type.decode('utf-8') == '安全运维'.decode('utf-8'):
tt=4
elif type.decode('utf-8') == '系统更新/维护'.decode('utf-8'):
tt=5
elif type.decode('utf-8') == '系统重启'.decode('utf-8'):
tt=6
elif type.decode('utf-8') == '系统迁移'.decode('utf-8'):
tt=7
elif type.decode('utf-8') == '系统扩容'.decode('utf-8'):
tt=8
else:
tt=9
print tt
sql = "INSERT INTO xxxxxxxxxxx(id,type,wtms,wtly,wtjssj,clr,kssj,jssj,clbf,memo,machinaroom_id) values(%d,%d,'%s','%s','%s','%s','%s','%s','%s','%s',%d)" % (0,tt,wtms,wtly,wtjssj,clr,kssj,jssj,clbf,"",33)
print sql
cursor.execute(sql)
db.commit()
except:
print "bbb"
db.rollback()
db.close()

上一篇:安装 Logstash (本地及 Docker) - Elastic Stack 实战手册


下一篇:[雪峰磁针石博客]flask构建自动化测试平台3-模板