如何用Python读写CSV文件?

如何用Python读写CSV文件?

我有一份文件example.csv有内容

1,"A towel,",1.042," it says, ",2.01337,is about the most ,-10,massively useful thing ,123-2,an interstellar hitchhiker can have.,3

我怎么看这个?example.csv和Python?

同样,如果我有

data = [(1, "A towel,", 1.0),
        (42, " it says, ", 2.0),
        (1337, "is about the most ", -1),
        (0, "massively useful thing ", 123),
        (-2, "an interstellar hitchhiker can have.", 3)]

我怎么写data到带有Python的CSV文件中?


慕的地10843
浏览 530回答 3
3回答

桃花长相依

编写CSV文件首先,您需要导入CSV例如:import csvwith 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'])

潇潇雨雨

import csvwith open(fileLocation+'example.csv',newline='') as File: #the csv file is stored in a File object     reader=csv.reader(File)       #csv.reader is used to read a file     for row in reader:         print(row)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python