# coding:utf-8 import pymysql import xlrd def dbconnect(): db = pymysql.connect( host="127.0.0.1", db="xx", user="root", passwd="root", charset='utf8', port=3306, use_unicode=True) cur = db.cursor() return db, cur def save(): db, cur = dbconnect() file_name = "bug.txt" with open(file_name,'r',encoding="UTF-8") as f: data=f.read() print(data) data_list=data.split(' ') n=20007 for nn in data_list: print(nn) sql = "insert into table(type,name) values (%s,%s);" try: cur.execute(sql, (str(n),nn.strip())) db.commit() n+=1 except Exception as e: print('erro:',e) save()