标签:执行 控制 ldd pass int script count for mit
# 1、写一个函数,随便输入一个表名,把这个表里面所有的数据,导出到excel里面
# 1、‘select * from %s‘ ,查出这个表所有的数据
# 2、再把所有的数据写到excel xlwt
import pymysql, hashlib, xlwt
def op_mysql(sql: str):
mysql_info = {
‘host‘: ‘***.24.3.40‘,
‘port‘: 3306,
‘password‘: ‘****‘,
‘user‘: ‘jxz‘,
‘db‘: ‘jxz‘,
‘charset‘: ‘utf8‘,
‘autocommit‘: True
}
result = ‘执行完成‘
conn = pymysql.connect(**mysql_info)
# cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标
cur = conn.cursor() # 建立游标
cur.execute(sql)
field = [t[0] for t in cur.description] # 获取表里面的所有字段名
if sql.strip().lower().startswith(‘select‘):
# result = cur.fetchone()
result = cur.fetchall()
cur.close()
conn.close()
print(‘所有的字段‘, field)
print(‘所有的数据‘, result)
result = list(result) # 因为返回的是元组,不能修改,所以转成list
result.insert(0, field) # 加入到最前面
return result
def export_excel(table_name):
sql = ‘select * from %s;‘ % table_name
result = op_mysql(sql)
book = xlwt.Workbook()
sheet = book.add_sheet(‘sheet‘)
for row, line in enumerate(result):
for col, t in enumerate(line):
sheet.write(row, col, t)
book.save(‘%s.xls‘ % table_name)
export_excel(‘CJ_test‘)
export_excel(‘app_myuser‘)
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet(‘sheet‘)
# # sheet.write(0,0,‘xxx‘)
#
# #nhy_user id ,username,passwd,error_count
#
l = [
[1, ‘dsk‘, ‘xxdfsdfsd‘, 0],
[2, ‘ldd‘, ‘xxdfsdfsd‘, 0],
[3, ‘lsdd‘, ‘xxdfsdfsd‘, 0],
[4, ‘lsd1‘, ‘xxdfsdfsd‘, 0],
]
# row = 0#行号
# for line in l: #外面这个循环,每循环一次,就写excel里面一行
# # col = 0#列号 [1,‘dsk‘,‘xxdfsdfsd‘,0]
# col = 0
# for t in line:#里面这个循环是控制列的,每循环一次就写一列
# sheet.write(row,col,t)
# col+=1
# row+=1
#
for row, line in enumerate(l):
for col, t in enumerate(line):
sheet.write(row, col, t)
book.save(‘user.xls‘)
标签:执行 控制 ldd pass int script count for mit
原文地址:https://www.cnblogs.com/skyxiuli/p/10848364.html