天天看點

國民經濟行業分類與代碼GB/T 4754-2002、GB/T 4754-2011、GB/T 4754-2017 存入MySQL資料庫

如果需要标準的國民經濟行業分類和代碼excel表,請+q:1770996260 備注:行業代碼

2002标準表格式示例

國民經濟行業分類與代碼GB/T 4754-2002、GB/T 4754-2011、GB/T 4754-2017 存入MySQL資料庫

2002資料庫

注意事項:字段的長度要保證足夠長。

國民經濟行業分類與代碼GB/T 4754-2002、GB/T 4754-2011、GB/T 4754-2017 存入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 count=0     #計算四位編碼個數
48 def into_mysql(filename):
49     category_code = ""      #門類編碼
50     category_name = ""      #門類名稱
51     global count
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             count=count+1
68             small_class=name        #小類名稱
69             new_code_2=code[:2]     #分割出兩位編碼    之後确定大類名稱
70             new_code_3=code[:3]     #分割出三位編碼    之後确定中類名稱
71             print(category_code)    #最終的字元串需要門類的編碼ABCD和門類的名稱
72             print(new_code_2)
73             print(new_code_3)
74             for j in range(len(df.index.values)):   #第二次周遊 尋找不同的位數的編碼對應不同的名稱
75                 if new_code_2==df.loc[j][0]:
76                     big_class=df.loc[j][1]    #大類名稱
77                 if new_code_3==df.loc[j][0]:
78                     mid_class=df.loc[j][1]    #中類名稱
79             tempres.append(category_code+code)              #清單暫存A0511 編碼
80             tempres.append(category_name+"·"+big_class+"·"+mid_class+"·"+small_class)   #清單暫存完整的名稱
81             print(tempres)
82             #====================================================================================
83             SQL = "insert into std_code_2017 (code,name) values('"+tempres[0]+"','"+tempres[1]+"');"     #sql插入語句
84             cursor.execute(SQL)             #執行sql語句
85             conn.commit()                   #送出事務
86             print("--------------------------------------------------")
87             # ====================================================================================
88             tempres=[]          #清空清單
89     close_conn(conn,cursor)     #關閉資料庫連接配接
90     print("所有的四位編碼數:\n",count)
91     return None
92 if __name__ == '__main__':
93     filename="GBT4754-2017.xlsx"
94     into_mysql(filename)      
國民經濟行業分類與代碼GB/T 4754-2002、GB/T 4754-2011、GB/T 4754-2017 存入MySQL資料庫

資料庫表

國民經濟行業分類與代碼GB/T 4754-2002、GB/T 4754-2011、GB/T 4754-2017 存入MySQL資料庫