103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
# -*- encoding:utf-8 -*-
|
||
|
||
'''
|
||
@Author : dingjiawen
|
||
@Date : 2023/5/20 16:32
|
||
@Usage :
|
||
@Desc : 随机向维度表中插入数据
|
||
'''
|
||
|
||
import pymysql
|
||
import pandas as pd
|
||
from pymysql.connections import Connection
|
||
import os
|
||
import random
|
||
import shutil
|
||
import math
|
||
|
||
root = r"E:\面试准备\interview-prepare\个人总结\04大数据\09项目\大型旋转机组健康管理系统软件\数仓\数仓建设\配置类数据库建立\20230327092414/"
|
||
root_file = os.path.join(root, "data_csv\\")
|
||
|
||
if not os.path.exists(root_file):
|
||
os.makedirs(root_file)
|
||
|
||
|
||
def nameChange():
|
||
file_name = os.listdir(root)
|
||
typeIdList = set()
|
||
for file in file_name:
|
||
if file.endswith(".xlsx"):
|
||
final_name = file.split(".")[0] + ".csv"
|
||
typeIdList.add(file.split("_")[1])
|
||
srcfile = os.path.join(root, file)
|
||
print(srcfile)
|
||
ex = pd.read_excel(srcfile)
|
||
tarfile = os.path.join(root_file, final_name)
|
||
ex.to_csv(tarfile, encoding="utf-8")
|
||
|
||
# shutil.copy(srcfile, tarfile)
|
||
# print("copy %s -> %s" % (srcfile, tarfile))
|
||
|
||
|
||
def loadData():
|
||
file_name = os.listdir(root_file)
|
||
typeIdList = set()
|
||
for file in file_name:
|
||
read_name = os.path.join(root_file + file)
|
||
# print(read_name)
|
||
data = pd.read_csv(read_name, encoding='utf-8')
|
||
for name in data['ORIGINAL_TAG']:
|
||
if type(name) is str and len(name) > 5:
|
||
# print(name.split("].")[0])
|
||
typeIdList.add(name.split("].")[0].split(".")[2])
|
||
break
|
||
# print(data)
|
||
print(typeIdList)
|
||
return typeIdList
|
||
|
||
pass
|
||
|
||
|
||
def getConnection():
|
||
# 打开数据库连接
|
||
try:
|
||
conn = pymysql.connect(host='Ding202', user='root', passwd='123456', port=3306, database='rt_phm_table')
|
||
print('连接成功!')
|
||
except:
|
||
print('something wrong!')
|
||
return conn
|
||
|
||
|
||
def insert_type_info(conn: Connection, if_clear=True):
|
||
typeIdList = loadData()
|
||
usersvalues = []
|
||
for typeId in typeIdList:
|
||
usersvalues.append(
|
||
(typeId, random.randint(1000, 2000), random.randint(40000, 50000), random.randint(10000, 20000),
|
||
random.randint(100, 200), random.randint(100, 300), random.randint(100, 500), random.randint(100, 500)))
|
||
cs = conn.cursor() # 获取光标
|
||
if if_clear:
|
||
cs.execute("truncate table gas_type_info")
|
||
# 注意这里使用的是executemany而不是execute,下边有对executemany的详细说明
|
||
cs.executemany(
|
||
"insert into gas_type_info values(%s,%s,%s,%s,%s,%s,%s,%s)",
|
||
usersvalues)
|
||
|
||
conn.commit()
|
||
cs.close()
|
||
conn.close()
|
||
print('OK')
|
||
|
||
|
||
def insert_data():
|
||
conn = getConnection()
|
||
|
||
insert_type_info(conn)
|
||
pass
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# nameChange()
|
||
# loadData()
|
||
insert_data()
|