国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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ù)庫的評論 (共 條)

分享到微博請遵守國家法律
夏河县| 波密县| 海阳市| 临武县| 乳源| 南充市| 双流县| 额敏县| 日喀则市| 溧阳市| 房山区| 永嘉县| 广平县| 会宁县| 陆川县| 宁海县| 临洮县| 晋州市| 筠连县| 新沂市| 牟定县| 南开区| 奇台县| 柘荣县| 齐河县| 临泉县| 新宾| 招远市| 玉山县| 庄浪县| 嘉黎县| 长岭县| 灵宝市| 微博| 惠安县| 云龙县| 阿坝| 姜堰市| 开封县| 盐边县| 越西县|