如果需要标准的国民经济行业分类和代码excel表,请+q:1770996260 备注:行业代码
2002标准表格式示例
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL3gDN5EDNzEDOx0yN4QTOwcTMwIzNyATMxIDMy0CM4ADM5AjMvwFMxEjMwIzLcBDOwATOwIzLcd2bsJ2Lc12bj5ycn9Gbi52YuAjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
2002数据库
注意事项:字段的长度要保证足够长。
代码:
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)