1、选择csv文件夹,遍历这个文件夹下的所有csv文件,csv文件名作为表名。这个文件夹下只能有csv文件,不能继续往下遍历文件夹。
2、可以设置表名对应的表注释 get_table_comment(),有需要自己设置。
3、可以设置csv表头对应的标准列名 get_en(),有需要自己设置。
打算出的功能:
1、直接把csv中的数据导入到MySQL表中
2、Hive版本
import pandas as pd
# import easygui as g # 导入EasyGui模块
import getpass
import os
import tkinter as tk
from tkinter import filedialog
def get_sql(table_path):
# 根据文件完整路径获取表名:I:\Codes\02-Python\pythonProject\csv\market_quotations_pig.csv
path = table_path.replace('\\', '/')
path_split = path.split('/')
table_name = path_split[len(path_split) - 1].split('.')[0]
# 读取csv,设置自己的csv编码格式
try:
csv = get_unicode(path)
except UnicodeDecodeError as e:
notion = "类型选择错误,请重新选择\n" + str(e)
print(notion)
csv = get_unicode(path)
df = pd.DataFrame(csv)
# 获取表头(列名)
columns = df.columns
print(columns)
# 获取注释
comment = get_table_comment(table_name)
# comment = input('请输入表说明')
# 创建SQL
sql = "create table " +"`"+ table_name+"`" + "(\n"
index = 0
for i in columns:
index += 1
cn_index = i
cn_index = get_en(cn_index)
# print(cn_index)
# 判断表头是否有对应的标准列名
if cn_index is None:
if index == len(columns):
sql = sql + " " + i + ' varchar(30)\n'
else:
sql = sql + " " + i + ' varchar(30),\n'
else:
if index == len(columns):
sql = sql + " " + cn_index + ' varchar(30)\n'
else:
sql = sql + " " + cn_index + ' varchar(30),\n'
char = ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ' + 'comment=' + "'" + comment + "'" + ';\n\n\n'
sql_final = sql + char
print(sql_final)
user = getpass.getuser()
sql_path = "C:/Users/" + user + "/Desktop/result.sql"
with open(sql_path, encoding='utf-8', mode='a') as fp:
fp.write(sql_final)
fp.close()
print("SQL存储在 %s中" % sql_path)
# 通过字典获取表名对应的标准列名
def get_en(cn):
index = {
"日期": "date",
"汇率": "rate",
"国家": "country"
}
return index.get(cn, None)
# 获取表注释
def get_table_comment(table_name):
table_comment = {
"market_quotations_AR_US_dollar_exchange_rate": "阿根廷美元汇率",
"market_quotations_BR_US_dollar_exchange_rate": "巴西雷亚尔美元汇率",
"market_quotations_CN_US_dollar_exchange_rate": "人民币美元汇率",
"market_quotations_CAB_CPI": "中国巴西阿根廷cpi指数",
"market_quotations_CAB_GDP": "中国巴西阿根廷年度GDP增速"
}
return table_comment.get(table_name, None)
# 设置读取csv的编码格式
def get_unicode(path):
# s = input("请选择文件编码格式\n1:utf-8\n2:gb2312\n")
# s = int(s)
#
# if s == 1:
# csv = pd.read_csv(path, encoding='utf-8')
# else:
# csv = pd.read_csv(path, encoding='gb2312')
csv = pd.read_csv(path, encoding='gb2312')
return csv
def get_table_path():
# 设置路径,通过窗口选择文件夹
# '''打开选择文件夹对话框'''
root = tk.Tk()
root.withdraw()
path = filedialog.askdirectory() # 获得选择好的文件夹
# path = g.fileopenbox('open file', '请选择目标文件')
for root, dirs, files in os.walk(path):
for file in files:
# 获取文件路径
table_path = root + '/' + file
get_sql(table_path)
if __name__ == '__main__':
get_table_path()
通过窗口选择文件夹
# 当导入tkinter模块后,调用 Tk()方法可初始化一个根窗体实例 root ,
# 用 title() 方法可设置其标题文字
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
# 隐藏窗口
root.withdraw()
# 获取文件夹路径
path = filedialog.askdirectory() # 获得选择好的文件夹
import os
# path是一个文件夹路径
for root, dirs, files in os.walk(path):
for file in files:
# 获取文件路径
table_path = root + '/' + file
get_sql(table_path)
root:是文件夹路径。
files:是文件夹下所有csv文件
get_en()、get_table_comment()
使用了字典,字典名.get(Key,None)
如果有对应的,则返回对应的Value,如果没有对应的,返回None
path = table_path.replace('\\', '/')
path_split = path.split('/')
table_name = path_split[len(path_split) - 1].split('.')[0]
有的是GBK,有的是utf-8,我这边给了一个选择
源码上我给写死了,解开注释就好了
get_sql():
# 读取csv,设置自己的csv编码格式
try:
csv = get_unicode(path)
except UnicodeDecodeError as e:
notion = "类型选择错误,请重新选择\n" + str(e)
print(notion)
csv = get_unicode(path)
def get_unicode(path):
# s = input("请选择文件编码格式\n1:utf-8\n2:gb2312\n")
# s = int(s)
#
# if s == 1:
# csv = pd.read_csv(path, encoding='utf-8')
# else:
# csv = pd.read_csv(path, encoding='gb2312')
csv = pd.read_csv(path, encoding='gb2312')
return csv
后面就是一些比较细碎的创建SQL语句,可自行查看
# 获取当前系统用户
user = getpass.getuser()
# 设置文件路径
sql_path = "C:/Users/" + user + "/Desktop/result.sql"
# 写入
with open(sql_path, encoding='utf-8', mode='a') as fp:
fp.write(sql_final)
fp.close()
print("SQL存储在 %s中" % sql_path)