批量處理excel到sql中

2021-09-26 10:11:15 字數 2835 閱讀 2419

import xlrd

from pymysql import *

import re

import os,shutil

import time

#處理檔案,把g:/2023年更新2023年樓盤/資料夾下打不開或者格式不結構的檔案移動到g:/新建資料夾/下

def wenjianchuli():

file_dir='g:/2023年更新2023年樓盤/'

for root, dirs, files in os.walk(file_dir):

# print(root) # 當前目錄路徑

# print(dirs) # 當前路徑下所有子目錄

# print(files) # 當前路徑下所有非目錄子檔案

for x in files:

path = root + x

print(path)

#成功開啟並列印成功,說明該檔案可處理

try:

#開啟檔案

data=xlrd.open_workbook(path)

#查詢工作表

sheets=data.sheets()

sheet_1_by_index=data.sheet_by_index(0)

row_values=sheet_1_by_index.row_values(1)

# col_values=sheet_1_by_index.col_values(1)

print(row_values)

#否則,不能處理的檔案移動到新建資料夾

except:

taget = 'g:/新建資料夾/'

shutil.move(path,taget+x)

print('移動{}成功'.format(str(x)))

time.sleep(4)

#資料處理成功的先拿到另外的資料夾下去,免得程式中斷要重頭跑

def success_move(file,path):

taget = 'g:/2023年更新2023年樓盤副/'

fils = str(file)

shutil.move(path, taget + fils)

print('移動{}成功'.format(fils))

time.sleep(4)

#處理資料

def shujuchuli():

file_dir='g:/2023年更新2023年樓盤/'

for root, dirs, files in os.walk(file_dir):

# print(root) # 當前目錄路徑

# print(dirs) # 當前路徑下所有子目錄

# print(files) # 當前路徑下所有非目錄子檔案

for file in files:

path = root + file

print(file)

print(path)

#開啟檔案

data=xlrd.open_workbook(path)

#查詢工作表

sheets=data.sheets()

# 可以通過函式、索引、名稱獲得工作表。

# sheet_1_by_function=data.sheets()[0]

# sheet_1_by_index=data.sheet_by_index(0)

sheet_1_by_index = data.sheet_by_index(0)

# 可以通過方法獲得某一列或者某一行的數值。

row_values=sheet_1_by_index.row_values(1)

col_values=sheet_1_by_index.col_values(1)

# print(row_values,col_values)

# 通過工作表的屬性獲得行數和列數。

n_of_rows=sheet_1_by_index.nrows

n_of_cols=sheet_1_by_index.ncols

# print(n_of_rows,n_of_cols)

conn = connect(host='127.0.0.1', port=3306, user='root', passwd='', db='shuju', charset='utf8')

# 也可以用乙個迴圈來遍歷一次檔案。

for i in range(n_of_rows):

masage=sheet_1_by_index.row_values(i)

print(masage)

for x in range(n_of_cols):

try:

#如果轉整數成功,說明是數字

number=int(masage[x])

#如果數字長度等於11,有可能是**

number=str(number)

if len(number)==11:

print(number)

print('成功')

cursor = conn.cursor()

query = cursor.execute("insert into shuju(telephone)values(%s)", (number))

conn.commit()

except:

print(masage[x])

print('失敗')

# conn.close()

success_move(file, path)

if __name__ == '__main__':

# wenjianchuli()

shujuchuli()

委託批量處理Excel

在以前的博文中 cad批量處理工具 batchproc,即只要使用者輸入處理單個檔案的 即可批量處理多個檔案。使用起來特別方便。在現在的地籍處理中,處理excel的情況比較多,尤其需要反反覆覆修改,那些做資料的真是傷不起!他們資料的特點是 1 在某乙個環節上,才發現上乙個環節出了問題,得瞞著領導來補...

委託批量處理Excel

在以前的博文中 cad批量處理工具 batchproc,即只要使用者輸入處理單個檔案的 即可批量處理多個檔案。使用起來特別方便。在現在的地籍處理中,處理excel的情況比較多,尤其需要反反覆覆修改,那些做資料的真是傷不起!他們資料的特點是 1 在某乙個環節上,才發現上乙個環節出了問題,得瞞著領導來補...

委託批量處理Excel

在以前的博文中 cad批量處理工具 batchproc,即只要使用者輸入處理單個檔案的 即可批量處理多個檔案。使用起來特別方便。在現在的地籍處理中,處理excel的情況比較多,尤其需要反反覆覆修改,那些做資料的真是傷不起!他們資料的特點是 1 在某乙個環節上,才發現上乙個環節出了問題,得瞞著領導來補...