轉(zhuǎn)載自:http://blog.csdn.net/pfm685757/article/details/47806469
如果不帶newline=‘’,你會(huì)發(fā)現(xiàn)也能寫(xiě)入結(jié)果芽死,但是每行內(nèi)容之間總是會(huì)多出一個(gè)空行
下面是python參考手冊(cè)里的解釋,在windows這種使用\r\n的系統(tǒng)里屯耸,不用newline=‘’的話护锤,會(huì)自動(dòng)在行尾多添加個(gè)\r,導(dǎo)致多出一個(gè)空行怒详,即行尾為\r\r\n
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.