2

I've managed to convert a .mdb file to .csv, based on this post: How to export MS Access table into a csv file in Python using e.g. pypyodbc.

However, I can not get the metadata (column name) from the original file. Does anyone have a clue on how to do that?

Thanks!

1

1 Answer 1

3

Simply retrieve headers from cursor.description where you will call writerow just before iterating through cursor results:

# OPEN CSV AND ITERATE THROUGH RESULTS
with open('CSVDatabaseWithHeaders.csv', 'w', newline='') as f:
    writer = csv.writer(f)    
    # ADD LINE BEFORE LOOP
    writer.writerow([i[0] for i in cur.description])  

    for row in cur.fetchall() :
        writer.writerow(row)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.