# coding:utf-8
import pypyodbc
import pandas as pd
def get_all_tables(connect_str):
conn = pypyodbc.win_connect_mdb(connect_str)
cur = conn.cursor()
cols_list = list(zip(*cur.columns().description))[0]
print(cols_list)
table_info_list = cur.fetchall()
table_df = pd.DataFrame(table_info_list)
table_df.columns = cols_list
print(table_df[['table_name', 'column_name', 'type_name']])
result = table_df.groupby(['table_name'])['column_name'].apply(list).to_dict()
print(result)
return result
def get_table_data(connect_str, table_name, columns_list, write_csv=False):
conn = pypyodbc.win_connect_mdb(connect_str)
cur = conn.cursor()
sql = "select * from " + table_name
print(sql)
cur.execute(sql)
all_data = cur.fetchall()
cur.close()
conn.close()
print("****************开始导出数据****************")
if write_csv:
export(all_data, columns_list, table_name + "_all_data")
def export(data, columns, file_name):
df = pd.DataFrame(data, columns=columns)
df.to_csv('./{}.csv'.format(file_name), encoding="utf-8-sig", index=False)
if __name__ == '__main__':
connect_str = "D:\\Users\\Downloads\\xxx.mdb;PWD=%s" % ("password")
all_table_dict = get_all_tables(connect_str)
for table_name, columns_list in all_table_dict.items():
get_table_data(connect_str, table_name, columns_list, True)
Python读取mdb文件数据
最新推荐文章于 2024-07-04 09:56:42 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)