Python csv file open error: _csv. Error:line contains NULL byte

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.