Python3 Processing GB18030 garbled

Source: Internet
Author: User

About GB18030 encoding

    • GB 18030 wiki:https://zh.wikipedia.org/wiki/gb_18030

    • A single byte with a value from 0 to 0x7f.

    • DWORD, the value of the first byte is from 0x81 to 0xFE, and the second byte has a value from 0x40 to 0xFE (not including 0x7f).

    • Four bytes, the value of the first byte from 0x81 to 0xFE, the value of the second byte from 0x30 to 0x39, the third byte from 0x81 to 0xFE, and the fourth byte from 0x30 to 0x39.


" a way to handle decoding errors "

    • Error:

Unicodedecodeerror: ' GB18030 ' codec can ' t decode byte 0xff in position 129535:illegal multibyte sequence
    • Bytes.decode

    • Codecs.register_error Sample Example

    • Exception object: Unicodedecodeerror

    • Custom replace_errors:

Import codecs# GB18030 garbled handlerdef walkergb18030replacehandler (exc):p rint (' exc.start:%d '% exc.start) print (' Exc.end :%d '% exc.end) print (' exc.encoding:%s '% exc.encoding) print (' Exc.reason:%s '% exc.reason) text = ' for ch in exc.object[ Exc.start:exc.end]:p rint (' ch: ') print (ch) text + = (' 0x%02X '% ch) return (text, exc.end) # Register Custom Handlercodecs.register_ Error ("Myreplace", Walkergb18030replacehandler)


" Related reading "

About the Python3 code


Walker's Journal * * *


Python3 Processing GB18030 garbled

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.