How to export sqlite to CSV in Python without being formatted as a list?

What you’re currently doing is printing out the python string representation of a tuple, i.e. the return value of str(row). That includes the quotes and ‘u’s and parentheses and so on.

Instead, you want the data formatted properly for a CSV file. Well, try the csv module. It knows how to format things for CSV files, unsurprisingly enough.

with open('output.csv', 'w', newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['Column 1', 'Column 2', ...])
    writer.writerows(data)

The newline="" is apparently needed for correct escaping of newlines inside quoted fields.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)