您的位置:首页 > 财经 > 金融 > 黄山旅游攻略 2日游最佳路线_长春新建设_营销方式和渠道有哪些_怎样推广自己的商城

黄山旅游攻略 2日游最佳路线_长春新建设_营销方式和渠道有哪些_怎样推广自己的商城

2025/3/17 17:38:11 来源:https://blog.csdn.net/Melgards/article/details/146273069  浏览:    关键词:黄山旅游攻略 2日游最佳路线_长春新建设_营销方式和渠道有哪些_怎样推广自己的商城
黄山旅游攻略 2日游最佳路线_长春新建设_营销方式和渠道有哪些_怎样推广自己的商城

需求:
用于将mysql数据库中的几百张表导出成excel方式
表中有些字段的值是含有双引号和逗号值,比如json值
表中有些字段是汉字内容
导出的excel要求有表的列名
shell对于含有逗号和双引号的值会错乱分割
数据库中某些字段值是化学符号

import pymysql,os,csv,shutil
target_dir=r'C:\Users\XXX\Desktop\excel'
if  os.path.exists(target_dir):shutil.rmtree(target_dir)os.mkdir(target_dir)
else:os.mkdir(target_dir)myconn=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='rhtx')
table_sql="select table_name from information_schema.tables where table_schema='rhtx'"
with myconn.cursor() as cursor:cursor.execute(table_sql)table_name=list(cursor.fetchall())for name in table_name:file_name=name[0]+".csv"file_path = os.path.join(target_dir, file_name)sql='select * from '+'rhtx.'+name[0]with myconn.cursor() as cursor:cursor.execute(sql)rows=cursor.fetchall()with open(file_path, mode='w', newline='',encoding='gbk') as file:writer=csv.writer(file)writer.writerow([i[0] for i in cursor.description])writer.writerows(rows)
myconn.close()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com