Code Snippet: Tuple to CSV (good for MySQLdb output)
So I have to do a lot of "reports" for ppl at work where I need to quickly build files for them to use from active databases. I wrote this to pass in a million and a half things and quickly output them. The normal python mysqldb cursor returns a tuple, but this can also be used for arrays (but not dicts). Hope it's helpful for someone!
For usage, pass full filepath file name, an array or tuple of titles for the columns/fields and then the data from mysql itself.
def write_tuple_to_file(file_name,titles,tpl): f = csv.writer(open(file_name,'wb'), delimiter=',',quotechar='"') f.writerow(titles) for x in tpl: f.writerow(x)












