How do I create a CSV file from database in Python?

前端 未结 8 2036
南方客
南方客 2020-12-05 00:05

I have a Sqlite 3 and/or MySQL table named \"clients\"..

Using python 2.6, How do I create a csv file named Clients100914.csv with headers? excel dialect...

8条回答
  •  悲哀的现实
    2020-12-05 01:10

    Using the csv module is very straight forward and made for this task.

    import csv
    writer = csv.writer(open("out.csv", 'w'))
    writer.writerow(['name', 'address', 'phone', 'etc'])
    writer.writerow(['bob', '2 main st', '703', 'yada'])
    writer.writerow(['mary', '3 main st', '704', 'yada'])
    

    Creates exactly the format you're expecting.

提交回复
热议问题