從非Windows 平台存取Access 資料庫:Linux 和Mac
作為Python 用戶,在非Windows 系統上使用Access 資料庫可能會帶來挑戰。然而,有一些解決方案可以克服這種跨平台限制。
對於 Mac OS X 使用者來說,一個可行的選擇是 pandas_access。該程式庫提供了從 Python 腳本讀取和寫入 Access 資料庫的功能。要使用它,請按照以下步驟操作:
<code class="python">import pandas_access as mdb db_filename = 'my_db.mdb' # Listing tables for tbl in mdb.list_tables(db_filename): print(tbl) # Read a table df = mdb.read_table(db_filename, "MyTable")</code>
在Ubuntu 上,在使用pandas_access 之前,您可能需要安裝mdbtools 軟體包:
<code class="bash">sudo apt install mdbtools</code>
或者,請考慮將Access數據匯出到CSV 檔案。 Pyparsing 是一個用於解析資料的 Python 函式庫,可用來將 MDB 檔案轉換為 CSV 格式:
<code class="python">import pyparsing mdb_file = 'my_db.mdb' csv_file = 'data.csv' with open(mdb_file, 'rb') as m: data = m.read() parser = pyparsing.Word(pyparsing.alphas) records = parser.scanString(data) with open(csv_file, 'w') as f: for record in records: f.write(','.join(record))</code>
此方法可讓您使用 Python 中的標準 CSV 操作技術存取 Access 資料庫中的資料。
以上是如何從非 Windows 平台存取 Access 資料庫:適用於 Linux 和 Mac 的 Python 指南?的詳細內容。更多資訊請關注PHP中文網其他相關文章!