The normal CSV file reads as follows:
#Coding:utf-8ImportCsvcsvfilename='Demo.csv'PrintU'############### #获取某一行'with open (Csvfilename,'RB') as Csvfile:reader=Csv.reader (csvfile) rows= [row forRowinchReader]PrintRows[0], rows[1], rows[2], rows[3]PrintU'############### #获取某一列'with open (Csvfilename,'RB') as Csvfile:reader=Csv.reader (csvfile) column0= [Row[0] forRowinchReader]with Open (Csvfilename,'RB') as Csvfile:reader=Csv.reader (csvfile) Column1= [Row[2] forRowinchReader]PrintColumn0, Column1s= [A] forIinchColumn0:Printtype (i)#print u ' sum: ', sum (column0)New_column0 =column0.pop (0)PrintU'the deleted elements are:', New_column0PrintU'after the list is deleted:', Column0Printtype (column0) forIinchColumn0:PrintType (i)
Reading a CSV with ucs-2 le format (notepa++ open CSV) will cause an error: Python csv error:line contains NULL byte references the contents of this article
Https://stackoverflow.com/questions/4166070/python-csv-error-line-contains-null-byte
The code is as follows:
#Coding:utf-8ImportCSVImportCodecstwsfilename="Tws.csv"#Read RowsPrintU'############### #获取某一行'with Codecs.open (Twsfilename,'RB',"utf-16") as Csvfile:reader=Csv.reader (csvfile) Column1= [Row[0] forRowinchReader]PrintColumn1[0]PrintU'############### #获取某一列'with Codecs.open (Twsfilename,'RB',"utf-16") as Csvfile:reader= Csv.reader (CSVFile, delimiter='\ t') Reader.next ()#jumping down a line can be commented out primarily in order to remove the header rowColumn1 = [Row[1] forRowinchReader]PrintColumn1PrintMax (Column1)
Finally thank the great God for the reference of a lot of things can not be confused. Replace (' plus ',') Ah, save it, you can't even give you hundreds of of this kind of CSV you can save!
Python csv file open error: _csv. Error:line contains NULL byte