喵星软件园提供热门手机游戏下载,最新手机游戏攻略!

《Super Auto Pets》六本食物一览,

时间:2023-10-03 22:45:39 来源: 浏览:

「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》六本食物一览,
链接:https://www.miaoshengapp.cn/yxgl/78535.html
版权:文章转载自网络,如有侵权,请联系删除!
资讯推荐
《原神》诸景巡礼第四天玩法攻略
《原神》诸景巡礼第四天玩法攻略

原神是一款画质很高的游戏,那么其中的诸景巡

2023-06-01
《进击的汉字》听妈妈的话过关攻略解析一览
《进击的汉字》听妈妈的话过关攻略解析一览

进击的汉字更新后,常常会有《进击的汉字》听

2023-05-14
07072手游(07072手游盒子冰雪之城)
07072手游(07072手游盒子冰雪之城)

本篇文章给大家谈谈07072手游,以及07072手游

2023-08-31
崩坏3蔚蓝耀斑怎么样 蔚蓝耀斑属性
崩坏3蔚蓝耀斑怎么样 蔚蓝耀斑属性

崩坏3蔚蓝耀斑怎么样呢,蔚蓝耀斑属于联动武

2023-08-06
光遇8月7日大蜡烛堆在哪 光遇8月7日大蜡烛堆在哪里介绍2023
光遇8月7日大蜡烛堆在哪 光遇8月7日大蜡烛堆在哪里介绍2023

光遇8月7日大蜡烛堆在哪呢,光遇8月7日中是具

2023-08-13
给孩子玩的钢琴乐器好玩吗 给孩子玩的钢琴乐器玩法简介
给孩子玩的钢琴乐器好玩吗 给孩子玩的钢琴乐器玩法简介

期待已久的手游给孩子玩的钢琴乐器即将登陆

2022-12-27