python中有一個讀寫csv檔案的包,直接import csv即可。利用這個python包可以很友善對csv檔案進行操作,一些簡單的用法如下。
1. 讀檔案
csv_reader = csv.reader(open('data.file', encoding='utf-8'))for row incsv_reader:
print(row)
例如有如下的檔案
輸出結果如下
['0.093700','0.139771','0.062774','0.007698']
['-0.022711','-0.050504','-0.035691','-0.065434']
['-0.090407','0.021198','0.208712','0.102752']
['-0.085235','0.009540','-0.013228','0.094063']
可見csv_reader把每一行資料轉化成了一個list,list中每個元素是一個字元串。
2. 寫檔案
讀檔案時,我們把csv檔案讀入清單中,寫檔案時會把清單中的元素寫入到csv檔案中。
list = ['1', '2','3','4']
out = open(outfile, 'w')
csv_writer= csv.writer(out)
csv_writer.writerow(list)
可能遇到的問題:直接使用這種寫法會導緻檔案每一行後面會多一個空行。
解決辦法如下:
out = open(outfile, 'w', newline='')
csv_writer= csv.writer(out, dialect='excel')
csv_writer.writerow(list)
參考如下:
在stackoverflow上找到了比較經典的解釋,原來 python3裡面對 str和bytes類型做了嚴格的區分,不像python2裡面某些函數裡可以混用。是以用python3來寫wirterow時,打開檔案不要用wb模式,隻需要使用w模式,然後帶上newline=''。
In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling.
In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is:
outputfile=open("out.csv",'w',encoding='utf8',newline='')
encoding can be whatever you require, but newline='' suppresses text mode newline handling. On Windows, failing to do this will write \r\r\n file line endings instead of the correct \r\n. This is mentioned in the 3.X csv.reader documentation only, but csv.writer requires it as well.