Home > Backend Development > Python Tutorial > How can I access Microsoft Access databases (.accdb and .mdb files) in Python on Linux and Mac?

How can I access Microsoft Access databases (.accdb and .mdb files) in Python on Linux and Mac?

Barbara Streisand
Release: 2024-10-29 18:55:30
Original
989 people have browsed it

How can I access Microsoft Access databases (.accdb and .mdb files) in Python on Linux and Mac?

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>
    Copy after login
  • 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>
    Copy after login
  • Read a table from the database:

    <code class="python">df = mdb.read_table(db_filename, "MyTable")</code>
    Copy after login

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>
Copy after login

Ubuntu Installation Note:

If you encounter issues on Ubuntu, consider running:

<code class="sh">sudo apt install mdbtools</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template