How can I write the rows of a mysql select to a logfile so every row is on a newline and all columns are split by "|"?
I'm using python3.
I see I have given a bit to little info, sorry.
What I have is this:
query = 'SELECT * FROM `table1` where date < x'
cursor = db.cursor()
rows = cursor.fetchall()
filename = '/logs/data.log'
fp = open(filename, 'w', newline='\n')
myFile = csv.writer(fp, lineterminator='\n')
Here's a sketch of what I think you need:
import csv rows = cursor.fetchall() filename = '/logs/data.log' fp = open(filename, 'w') # newline='\n' not needed w = csv.writer(fp, lineterminator='\n', delimiter='|', quotechar='') w.writerows(rows)
fetchall returns a list of tuples. The
csv.writer documentation doesn't say, but I expect is takes any sequence of sequences.