Issue creating CSV from Access database

107 Views Asked by At

I have a database in MS Access. I am trying to export one column from one table to a CSV file, with Python using pypyodbc. From the CSV file obtained, there are no commas in numbers greater than 1. Any idea to solve?

Screen from MS Access: MS Access database

Screen from the obtained CSV: CSV

Code:

import pypyodbc
import csv
import os
from pathlib import Path
import re

data_folder1 = Path("/Users/sfulc/Desktop/FileProva/")
data_folder2 = Path("/Users/sfulc/Desktop/FileOutput/")

for filename in os.listdir("/Users/sfulc/Desktop/FileProva/"):
    file1 = r"Dbq=" + os.path.abspath(data_folder1 / filename) + r";"
    file2 = re.sub("mdb", "csv", os.path.abspath(data_folder2 / filename))
    pypyodbc.lowercase = False
    conn = pypyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + file1)
    cur = conn.cursor()
    cur.execute("SELECT LoadValue FROM OriginalData");
    with open(file2, 'w', newline='') as f:
        writer = csv.writer(f)
        for row in cur.fetchall():
            writer.writerow(row)
    cur.close()
    conn.close()
0

There are 0 best solutions below