
本文共 4378 字,大约阅读时间需要 14 分钟。
������������������Excel���������������������������������������������Python������������������������������������������������������Excel������������������������������������������������������������������
���������������������
���������������������
������PyMySQL���������������������������������������������������������������������������������������������������������������������������������������������import pymysqltry: db = pymysql.connect( host="127.0.0.1", # ������������������������ user="root", # ������������������ passwd="123456", # ��������������� db="fresh", # ��������������� charset="utf8" #������������������ )except: print("could not connect to mysql server")
������������������������������������������������������������������������
Excel���������������������
Excel������������
������Pybernate���xlrd���������Excel���������������������������������������������def open_excel(): try: book = xlrd.open_workbook(r'D:\db\area.xlsx') # ������������������������ except: print("open excel file failed!" ) try: sheet = book.sheet_by_name("���������������") return sheet except: print("locate worksheet in excel failed!")
���������������������������������������������Excel���������������������sheet���������������������
������������������
������Excel���������������������������������������������������������def insert_deta(): sheet = open_excel() cursor = db.cursor() row_num = sheet.nrows # ��������������� for i in range(1, row_num): # ������������������������ row_data = sheet.row_values(i) # ��������������������������� value = ( int(row_data[0]), # id int(row_data[1]), # pid row_data[2], # name int(row_data[23]) # level ) # ������������������ sql = "INSERT INTO area (id, pid, name, level) VALUES(%s, %s, %s, %s)" cursor.execute(sql, value) db.commit() cursor.close()
���������������������
���������������������������������������������������������SQL���������������������������������������# ������������������SQL������������������cursor.execute(sql, (value[0], value[1], value[2], value[3]))
������������������������
���������������������
������PyMySQL������������������������������������������- server_address: ������������������IP������
- database_name: ������������������������
- charset: ���������������������������utf-8
Excel������������������
������xlrd���������Excel������������������������������������- ������������������������
- ������������������������������������������������
- ���������������������������������������������
������������������
���������������������������������������������������������- ���������������������
- ���������������������
- execute������SQL������
- ������������������
���������������������
������������������������
���������������������������������Python������pip install pymysql xlrd
���������������������
���������������������������������������������������������������user = "���������username" # ������������������������passwd = "���������������" # ���������������������
������������������
������Excel���������������������������������������������������������������������������Python������������������������������������������������������������������������
- ���������������������������������������������������Excel���������������������������
- ������������������������������������Python���������������������������������������������������������
- ������������������������������������������������������������������������������������
���������������������
������������������
���������������������������������Excel���������������������������������������������������������������������������������������
���������������������������������������������������������������������# ������������������������������������print(f"���������������{i}������������{row_data}")
������������������
- ���������������������������������������������������
- ������������������������������������������������������������
- ������Excel���������������������������������������
������������������������������������������������Excel���������������������������������������������������������������������������Excel���������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
