时间:2021-07-01 10:21:17 帮助过:6人阅读
import pymysql import xlwt def get_sel_excel(sql): ‘‘‘ 连接mysql并把查询出来的数据写入excel表格 :param sql: :return: ‘‘‘ conn = pymysql.connect( host="localhost", port=3306, user="root", passwd="123", db="db_test", charset="utf8mb4" ) # 建立游标 cursor = conn.cursor() print("开始查询表!") # 执行sql语句 cursor.execute(sql) # 获取查询到结果 res = cursor.fetchall() print(res) w_excel(res) def w_excel(res): ‘‘‘ 把数据写入excel表格中 :param res: :return: ‘‘‘ book = xlwt.Workbook() # 新建一个excel sheet = book.add_sheet(‘vehicle_land‘) # 新建一个sheet页 title = [‘平台货号‘, ‘商品名称‘, ‘售价‘] # 写表头 i = 0 for header in title: sheet.write(0, i, header) i += 1 # 写入数据 for row in range(1, len(res)): for col in range(0, len(res[row])): sheet.write(row, col, res[row][col]) row += 1 col += 1 book.save(‘vehicle_land.xls‘) print("导出成功!") if __name__ == "__main__": sql = ‘select goods_commonid,goods_name,goods_price from mall_goods_common where store_id=110 and set_new_time>1560182400;‘ get_sel_excel(sql)
pandas把读取sql的结果写入到excel文件
标签:读取 encoding cal _id 成功 res mysql数据库 user 表头