五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

將excel表里面的數(shù)據(jù)(str居多)進行邏輯運算后存入MySQL數(shù)據(jù)庫

2023-07-24 22:32 作者:一個無昵稱的人  | 我要投稿

標題就是需求,使用語言是python,用到了pymysql庫和pandas庫(沒有的話自行pip install),代碼如下:


import pymysql

import pandas as pd


# 連數(shù)據(jù)庫

conn = pymysql.connect(host='localhost', user='root', password='0000', db='p1')


# 創(chuàng)建列表

excel_file = pd.ExcelFile('數(shù)組.xlsx')

df1 = pd.read_excel(excel_file, sheet_name='Sheet1')

df2 = pd.read_excel(excel_file, sheet_name='Sheet2')


# 將df1和df2轉(zhuǎn)換為列表

list1 = df1.values.tolist()

list2 = df2.values.tolist()


# 進行數(shù)據(jù)對比并更新列表

i = 0

j = 0

while i < len(list1):

? ? print('第' + str(i) + f'行,進度{round(i / 24956 * 100, 1)}%')

? ? while j < len(list2):

? ? ? ? if str(list1[i][0]) == str(list2[j][0]) and str(list1[i][1]) == str(list2[j][1]) and (str(list1[i][5]) == str(list2[j][3]) or str(list1[i][6]) == str(list2[j][5])) and str(list1[i][8]) == str(list2[j][9]):

? ? ? ? ? ? list1[i][13] = list2[j][15]

? ? ? ? ? ? list1[i][14] = list2[j][16]

? ? ? ? ? ? list1[i][15] = list2[j][17]

? ? ? ? ? ? list1[i][16] = list2[j][18]

? ? ? ? ? ? list1[i][17] = list2[j][19]

? ? ? ? ? ? list1[i][18] = list2[j][20]

? ? ? ? j += 1

? ? j = 0

? ? i += 1


# 創(chuàng)建列表的idx用來給sql提供idx

idxs = []

columns_sql = ""

for idx in df1.columns:

? ? idxs.append(str(idx))

? ? print(idx)

? ? columns_sql += f"{str(idx)} VARCHAR(255) NOT NULL, "


print(len(idxs))

# 創(chuàng)建一個游標對象

cursor = conn.cursor()


# 定義創(chuàng)建表的SQL語句

sql = f'''CREATE TABLE StudyTable (

? ? ? ? ? ? {columns_sql[:-2]},

? ? ? ? ? ? PRIMARY KEY ({idxs[0]})

? ? ? ? ? ? )'''


# 生成數(shù)據(jù)表

cursor.execute(sql)


# 將二維列表插入到MySQL表中

placeholders = ', '.join(['%s'] * len(idxs))

insert_sql = f"INSERT INTO StudyTable VALUES ({placeholders})"


# 執(zhí)行SQL語句

cursor.executemany(insert_sql, list1)


# 提交事務

conn.commit()


# 關閉連接

cursor.close()

conn.close()

將excel表里面的數(shù)據(jù)(str居多)進行邏輯運算后存入MySQL數(shù)據(jù)庫的評論 (共 條)

分享到微博請遵守國家法律
河津市| 大丰市| 南乐县| 甘肃省| 鹰潭市| 怀来县| 古浪县| 隆子县| 太和县| 车致| 通化县| 福泉市| 邯郸县| 耿马| 循化| 东乌| 喀喇| 芦溪县| 镇安县| 镇雄县| 凌源市| 宝清县| 三都| 新巴尔虎右旗| 湘潭县| 卢氏县| 三亚市| 屯昌县| 玉屏| 万荣县| 辽阳县| 华宁县| 龙州县| 乐东| 三台县| 长治县| 临安市| 湘潭市| 繁昌县| 桐梓县| 井冈山市|