《Super Auto Pets》六本食物一览,
「Python」「Access」「Excel」客户信息收集程序
2022-11-10 客户需求如下:
实施步骤
1.按照pyodbc库
2.如果没有装过微软的access数据库的话,那么需要先安装access的驱动程序
32位:http://download.microsoft.com/download/E/4/2/E4220252-5FAE-4F0A-B1B9-0B48B5FBCCF9/AccessDatabaseEngine.exe
64位:http://download.microsoft.com/download/E/4/2/E4220252-5FAE-4F0A-B1B9-0B48B5FBCCF9/AccessDatabaseEngine_X64.exe
3.Excel表格
CustomerID | FName | LName | StateName | Gender | HHI | Marital | Children | HasPets |
2669 | Andrew | Sanchez | Montana | m | 102505 | married | 0 | n |
468 | Owen | Mitchell | Idaho | m | 164399 | single | 0 | n |
525 | Penelope | Johnson | New Mexico | f | 102258 | married | 3 | n |
530 | Barrett | Martinez | Colorado | m | 83000 | single | 1 | y |
738 | Chase | Harrington | Colorado | m | 84105 | married | 1 | n |
879 | Samuel | Atkinson | Wyoming | m | 133509 | single | 0 | y |
922 | Evie | O'Brien | Wyoming | f | 74435 | single | 0 | n |
993 | Raquel | Martin | Colorado | f | 72538 | married | 3 | y |
1452 | Jackson | Phillips | Wyoming | m | 163960 | single | 5 | n |
1457 | Samuel | Patterson | Idaho | |||||
1583 | Max | Jones | Utah | m | 61452 | married | 1 | y |
1695 | Justin | Decker | New Mexico | m | 357079 | single | 1 | y |
1714 | Brittany | Diaz | Utah | f | 76718 | married | 4 | y |
4.代码
#导入pyodbcimport pyodbcmdb_file=r'C:\\Users\\Administrator\\jupyter\\access\\Database1.accdb'driver = '{Microsoft Access Driver (*.mdb,*.accdb)}'conn = pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + DBfile + ";Uid=;Pwd=;")cur = conn.cursor()sql = "SELECT * FROM 表1"print(sql)cur.execute(sql)alldata = cur.fetchall()#导入pypyodbcimport pypyodbcmdb = 'Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:\\Users\\Administrator\\jupyter\\access\\Database1.accdb'conn = pypyodbc.win_connect_mdb(mdb)cur = conn.cursor()sql = "SELECT * FROM 表1"print(sql)cur.execute(sql)alldata = cur.fetchall()print(alldata)
#读取access文件mdb_file=r'C:\\Users\\Administrator\\jupyter\\access\\Database1.accdb'driver = '{Microsoft Access Driver (*.mdb,*.accdb)}'conn = pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + mdb_file + ";Uid=;Pwd=;")cur = conn.cursor()#cur.execute('DROP TABLE CustomerInfo ')#遍历ACCDB文件中的表名tables=<>for table_name in cur.tables(tableType="TABLE"): print(table_name.table_name) tables.append(table_name.table_name)print(tables)
# 创建access表的sql语句sql = """create table CustomerInfo(CustomerID AUTOINCREMENT PRIMARY KEY,FName varchar(255),LName varchar(255),StateName varchar(255),Gender varchar(255),HHI INTEGER,Marital varchar(255),Children INTEGER,HasPets varchar(255));"""if "CustomerInfo" not in tables: print(sql) cur.execute(sql)sql = "SELECT * FROM CustomerInfo"print(sql)cur.execute(sql)alldata = cur.fetchall()print(alldata)conn.commit()
#使用pandas读取excel表并对空值填充并删除重复的并重置indeximport pandas as pddf=pd.read_excel('Major Programming HW Dataset.xlsx')df<'Gender'>.fillna(" ",inplace=True) df<'HHI'>.fillna(0,inplace=True) df<'Marital'>.fillna(" ",inplace=True) df<'Children'>.fillna(0,inplace=True) df<'HasPets'>.fillna(" ",inplace=True) df.drop_duplicates(subset=<'CustomerID'>,keep='first',inplace=True)df.reset_index(inplace=True,drop=True)df
#遍历dataframe表insert Accesssql = "delete FROM CustomerInfo"print(sql)cur.execute(sql)for i in range(len(df)): sql=f"INSERT INTO CustomerInfo(CustomerID, FName,LName,StateName,Gender,HHI,Marital,Children,HasPets) VALUES (%d,'%s' ,'%s','%s','%s',%d,'%s',%d,'%s')"%(df.loc,df.loc,df.loc.replace("'",""),df.loc,df.loc,df.loc,df.loc,df.loc,df.loc) print(sql) cur.execute(sql)conn.commit()
#全部代码def f_driver(): return def d_driver(): mdb_file=r'C:\\Users\\Administrator\\jupyter\\access\\Database1.accdb' driver = '{Microsoft Access Driver (*.mdb,*.accdb)}' conn = pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + mdb_file + ";Uid=;Pwd=;") cur = conn.cursor() return curdef di_table(cur): tables=<> for table_name in cur.tables(tableType="TABLE"): #print(table_name.table_name) tables.append(table_name.table_name) return tablesdef cre_table(cur,tables): # 创建表的sql语句 sql = """ create table CustomerInfo ( CustomerID AUTOINCREMENT PRIMARY KEY, FName varchar(255), LName varchar(255), StateName varchar(255), Gender varchar(255), HHI INTEGER, Marital varchar(255), Children INTEGER, HasPets varchar(255) );""" if "CustomerInfo" not in tables: #print(sql) cur.execute(sql) sql1 = """ create table Customer ( CustID AUTOINCREMENT PRIMARY KEY, FName varchar(255), LName varchar(255), StateName varchar(255), HHI INTEGER, Marital varchar(255), Children INTEGER, HasPets varchar(255) );""" if "Customer" not in tables: #print(sql1) cur.execute(sql1)def dao_excel(cur): #使用pandas读取excel表并对空值填充并删除重复的并重置index #import pandas as pd df=pd.read_excel('Major Programming HW Dataset.xlsx') df<'Gender'>.fillna(" ",inplace=True) df<'HHI'>.fillna(0,inplace=True) df<'Marital'>.fillna(" ",inplace=True) df<'Children'>.fillna(0,inplace=True) df<'HasPets'>.fillna(" ",inplace=True) df.drop_duplicates(subset=<'CustomerID'>,keep='first',inplace=True) df.reset_index(inplace=True,drop=True) #遍历dataframe表insert Access sql = "delete FROM CustomerInfo" #print(sql) cur.execute(sql) for i in range(len(df)): sql=f"INSERT INTO CustomerInfo(CustomerID, FName,LName,StateName,Gender,HHI,Marital,Children,HasPets) VALUES (%d,'%s' ,'%s','%s','%s',%d,'%s',%d,'%s')"%(df.loc,df.loc,df.loc.replace("'",""),df.loc,df.loc,df.loc,df.loc,df.loc,df.loc) #print(sql) cur.execute(sql) conn.commit() return dfdef print_cut(cur): sql = "SELECT * FROM Customer" print(sql) cur.execute(sql) alldata = cur.fetchall() print(alldata)
-
《Super Auto Pets》六本食物一览, 2023-10-03
-
《Super Auto Pets》五本食物一览, 2023-10-03
-
《Super Auto Pets》三本食物一览, 2023-10-03
-
《subverse》战棋角色属性介绍 战棋角色属性怎么看, 2023-10-03
-
《subverse》战棋操作方法分享 战棋怎么操作, 2023-10-03
-
《subverse》工程师要塞保护方法分享 工程师要塞怎么保护, 2023-10-03
-
《subverse》优化方法介绍 怎么优化, 2023-10-03
-
《subverse》10月更新了什么, 2023-10-03
-
《Stray》配置要求一览 最低什么配置能玩, 2023-10-03
-
《steam》价格一览 帝国神话价格,帝国神话在steam上面要多少钱 2023-10-03