transcoding data using Python

Source: Internet
Author: User

Big data is the most annoying is the poor quality of data, in order to import data into the SEQUOIADB, need to require the text is UTF-8 mode, using eNCA to view the file encoding is gb2312, and then eNCA to utf-8 error. Google, the whole planet doesn't know why, try to transcode with Python

#-*-coding:utf-8-*-ImportCodecsImportSYSPrint "file name:", sys.argv[1]filename= Sys.argv[1]if(FileName = =None): Exit (1) file=open (filename) WriteFile= Open (filename+"UTF8","w+") BOM= File.read (3)if(BOM = =codecs. Bom_utf8): File.seek (3) forAinchFile:writefile.write (A.decode ("gb2312"). Encode ("Utf-8")) File.close () writefile.close ()
Here are two points: 1:for line in file is Python to read large files faster Method 2: The above code transcoding times wrong: unicodedecodeerror: ' gb2312 ' codec can ' t decode bytes in Positio N 2-3: Illegal multibyte sequence The second problem: Writefile.write into print does not have this problem, but a write there is a problem, very confusing, and then found an article on the internet said: the processing of characters is really gb2312, But the inclusion of some of the special characters in the gb2312 can not find the relevant mapping into Unicode, the use of GB18030, he is a superset of GB2312, contains a lot of special characters is gb2312 No, changed to GB18030 can be. However, there is still no reason why print can work.

transcoding data using Python

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.