Accessing Access Databases in Python on Non-Windows Platforms (Linux and Mac)
Question:
You wish to access data from Microsoft Access databases (.accdb and .mdb files) in Python, but you're encountering limitations with pyodbc on Mac OS X. Additionally, you're interested in the possibility of exporting the data to CSV format for further processing.
Answer:
Fortunately, when working on Mac OS X or Ubuntu 18.04, you can utilize the pandas_access library to access Access databases.
pandas_access Installation and Usage:
Install pandas_access using pip:
<code class="sh">pip install pandas_access</code>
Access the database and list its tables:
<code class="python">import pandas_access as mdb db_filename = 'my_db.mdb' for tbl in mdb.list_tables(db_filename): print(tbl)</code>
Read a table from the database:
<code class="python">df = mdb.read_table(db_filename, "MyTable")</code>
Exporting Data to CSV:
If desired, you can export the data to a CSV file using the to_csv() method:
<code class="python">df.to_csv('table_data.csv', index=False)</code>
Ubuntu Installation Note:
If you encounter issues on Ubuntu, consider running:
<code class="sh">sudo apt install mdbtools</code>
By leveraging pandas_access, you can effectively work with Access databases in Python on non-Windows platforms.
The above is the detailed content of How can I access Microsoft Access databases (.accdb and .mdb files) in Python on Linux and Mac?. For more information, please follow other related articles on the PHP Chinese website!