Python character encoding

Source: Internet
Author: User

Using standard library codecs modules

Codecs.open (filename, mode= ' R ', Encoding=none, errors= ' strict ', buffering=1)
1 import codecs2 f = codecs.open (filename, encoding= ' utf-8 ')

Read the Utf-8 file in this way above and automatically convert to Unicode. However, it must be clear that the file type is UTF8 type. If there is a Chinese character in the file, it is not a byte to read but all the bytes of the whole Chinese character are read in and converted to Unicode (conjecture is related to the UTF8 encoding of Chinese characters).

The code below is also a way to read and write using codecs

#coding =utf-8import codecsfin = open ("Test.txt", ' r ') Fout = open ("Utf8.txt", ' w ') reader = Codecs.getreader (' GBK ') (Fin) writer = Codecs.getwriter (' GBK ') (fout) data = Reader.read #10是最大字节数, the default value is 1, which means as large as possible. You can avoid processing large amounts of data at one time while data:    writer.write (data) Data    = Reader.read (10)

Python character encoding

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.