標准國民經濟行業分類與代碼GB/T 4754-2011存入mysql數據庫


國標:

代碼:

 1 import pandas as pd
 2 import pymysql
 3 """
 4 ------------------------------------------------------------------------------------
 5 """
 6 def get_conn():
 7     """
 8     :return: 連接,游標
 9     """
10     # 創建連接
11     conn = pymysql.connect(host="127.0.0.1",
12                     user="root",
13                     password="000429",
14                     db="data_cleaning",
15                     charset="utf8")
16     # 創建游標
17     cursor = conn.cursor()  # 執行完畢返回的結果集默認以元組顯示
18     return conn, cursor
19 
20 def close_conn(conn, cursor):
21     if cursor:
22         cursor.close()
23     if conn:
24         conn.close()
25 """
26 -----------------------------------------------------------
27 """
28 """
29 ------------------------------------------------------------------------------------
30 """
31 def query(sql,*args):
32     """
33     通用封裝查詢
34     :param sql:
35     :param args:
36     :return:返回查詢結果 ((),())
37     """
38     conn , cursor= get_conn()
39     print(sql)
40     cursor.execute(sql)
41     res = cursor.fetchall()
42     close_conn(conn , cursor)
43     return res
44 """
45 ------------------------------------------------------------------------------------
46 """
47 
48 def into_mysql(filename):
49     category_code = ""      #門類編碼
50     category_name = ""      #門類名稱
51 
52     conn,cursor=get_conn()  #連接mysql
53     if(conn!=None):
54         print("數據庫連接成功!")
55     tempres = []            #暫存列表
56     df=pd.read_excel(filename)      #讀取標准表
57     # print(len(df.index))
58     for i in range(len(df.index.values)):   #第一層遍歷標准表 找到門類的編碼和名稱 找到小類的編碼
59         # print(df.loc[i][1])
60         code=str(df.loc[i][0])           #所有的編碼
61         name=str(df.loc[i][1])           #所有的名稱
62         if len(code)==1:
63             category_code=code     #門類編碼
64             category_name=name     #門類名稱
65         #分割編碼
66         if len(code)==4:
67             small_class=name        #小類名稱
68             new_code_2=code[:2]     #分割出兩位編碼    之后確定大類名稱
69             new_code_3=code[:3]     #分割出三位編碼    之后確定中類名稱
70             print(category_code)    #最終的字符串需要門類的編碼ABCD和門類的名稱
71             print(new_code_2)
72             print(new_code_3)
73             for j in range(len(df.index.values)):   #第二次遍歷 尋找不同的位數的編碼對應不同的名稱
74                 if new_code_2==df.loc[j][0]:
75                     big_class=df.loc[j][1]    #大類名稱
76                 if new_code_3==df.loc[j][0]:
77                     mid_class=df.loc[j][1]    #中類名稱
78             tempres.append(category_code+code)              #列表暫存A0511 編碼
79             tempres.append(category_name+"·"+big_class+"·"+mid_class+"·"+small_class)   #列表暫存完整的名稱
80             print(tempres)
81             SQL = "insert into std_code (code,name) values('"+tempres[0]+"','"+tempres[1]+"');"     #sql插入語句
82             try:
83                 cursor.execute(SQL)             #執行sql語句
84                 conn.commit()                   #提交事務
85                 print(""+str(i+1)+"條數據插入成功:\n",category_code+code,name)        #插入成功輸出
86                 print("--------------------------------------------------")
87             except:
88                 print("插入失敗:\n",category_code+code,name)
89             tempres=[]          #清空列表
90     close_conn(conn,cursor)     #關閉數據庫連接
91     return None
92 if __name__ == '__main__':
93     filename="GBT4754-2011.xlsx"
94     into_mysql(filename)

運行代碼輸出的內容截圖:

 

 

最終存入mysql數據庫截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM