How do I read and write CSV files with Python?

后端 未结 4 1794
抹茶落季
抹茶落季 2020-11-21 23:14

I have a file example.csv with the contents

1,\"A towel,\",1.0
42,\" it says, \",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-21 23:19

    Writing a CSV file

    First you need to import csv

    For eg:

    import csv
    
    with open('eggs.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
        spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    

提交回复
热议问题