導入模塊
import csv
2、讀取csv文件
file1 = open('test1.csv', 'rb')
reader = csv.reader(file1)
rows = [row for row in reader] #csv文件每一行內容組成的二位列表
Python pandas模塊
3、寫入csv文件
csv.reader()返回一個DictReader對象。
csv.writer()返回一個DictWriter對象。
DictWriter.writerow()寫一行。
DictWriter.writerows()寫多行。
需要注意的問題是:當我們需要寫csv的時候,打開文件一定要帶上’b’,否則可能會往文件里輸出空行。Python 3.x情況會有些不同。