Answers for "pandas read mdb"

1

pandas read mdb

#### Using Pandas read_sql
import pyodbc
import pandas as pd

# set up some constants
# "." pwd + "\\" to overcome error 
MDB = '.\\Akbar.mdb'
DRV = '{Microsoft Access Driver (*.mdb, *.accdb)};'

## connect to db
con = pyodbc.connect('DRIVER={};DBQ={};'.format(DRV,MDB))

## read with pandas 
df = pd.read_sql('SELECT * FROM sheet2;',con)
df.head(5)
Posted by: Guest on September-28-2021
1

pandas read mdb

#### using pyodbc and csv module
import csv, pyodbc

# set up some constants
MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'

# connect to db
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()

# run a query and get the results 
SQL = 'SELECT * FROM mytable;' # your query goes here
rows = cur.execute(SQL).fetchall()
cur.close()
con.close()

# you could change the mode from 'w' to 'a' (append) for any subsequent queries
with open('mytable.csv', 'w') as fou:
    csv_writer = csv.writer(fou) # default field-delimiter is ","
    csv_writer.writerows(rows)
Posted by: Guest on September-28-2021

Python Answers by Framework

Browse Popular Code Answers by Language