下载标准国民经济行业分类与代码GB/T 4754-2011,存入mysql数据库

戳链接下载:https://download.csdn.net/download/weixin_45556024/34913490

或关注公众号【靠谱杨阅读人生】回复【行业】获取。

整理不易,资源fu费,谢谢理解!

国标:

代码:

 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数据库截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靠谱杨

感谢您的认可和鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值