Home Backend Development Python Tutorial python black magic encoding conversion method

python black magic encoding conversion method

Mar 13, 2017 pm 06:15 PM

This article mainly introduces the encoding conversion of python and analyzes the method of python encoding conversion. Interested friends can refer to it

We are using other languages When the library does encoding conversion, there are usually only two (or three) ways to deal with unintelligible characters:

  • throw an exception

  • Replace with alternative characters

  • Skip

But in the complex real world, due to various unreliability, there will always be some discordant factors in the texts we deal with, such as Mixed encoding. In this case, it’s back to the above approach.

Then the question is, is there a better way in python?

The answer is, yes!

Python's encoding conversion process is actually a two-stage conversion:


source -> unicode -> dest
Copy after login

First convert the string from the original encoding to unicode. Then convert unicode to the target encoding.

In the first step, we generally use decode() or unicode() these two functions Finish.
In the second step we use the encode() function to complete.

The black magic we are talking about here is realized in the first step. The

decode and unicode functions both have an optional parameter called errors. Take a look at the official description:

  • errors may be given to set a different error

  • handling scheme. Default is 'strict' meaning that encoding errors raise

  • ## a UnicodeDecodeError. Other possible values ​​are 'ignore' and 'replace'

  • ##as well as any other name registered with codecs . register_error that is

  • able to handle UnicodeDecodeErrors.

  • This parameter usually has three values:

  • strict default value. If an encoding error occurs, UnicodeDecodeError is thrown.

  • ignore Skip.

  • replace Replace with ?.

  • #Okay, did you see the last sentence? The show is on!

The module codec has a function called register_error. Its function allows users to register custom errors handling methods.

Used to handle UnicodeDecodeError.


Let’s take a look at the function prototype:

codecs.register_error(name, error_handler)
Copy after login

name:

The name of error handling

. Used to fill in the error parameter of the decode function. error_handler: processing function. This function accepts an exception parameter. Returns a tuple, which has 2 elements. The first is the error-corrected string, and the second is the starting position to continue decoding.

With the above basic concepts. Let’s take a look at the specific implementation:

def cjk_error(e):
  if not isinstance(e, UnicodeDecodeError):
    raise TypeError("don't know how to handle %r" % exc) 
  if exc.end + 1 > len(exc.object): 
    raise TypeError('unknown codec ,the object too short!') 
  ch1 = ord(exc.object[exc.start:exc.end]) 
  newpos = exc.end + 1 
  ch2 = ord(exc.object[exc.start + 1:newpos]) 
  sk = exc.object[exc.start:newpos] 
  if 0x81<=ch1<=0xFE and (0x40<=ch2<=0x7E or 0x7E<=ch2<=0xFE): # GBK 
    return (unicode(sk,&#39;cp936&#39;), newpos) 
  if 0x81<=ch1<=0xFE and (0x40<=ch2<=0x7E or 0xA1<=ch2<=0xFE): # BIG5 
    return (unicode(sk,&#39;big5&#39;), newpos) 
  raise TypeError(&#39;unknown codec !&#39;) 
codecs.register_error("cjk_replace", cjk_replace)
Copy after login

The above is what I

copy

from the Internet. I thought it was very good at first, but later I found out that it was a very unreflective algorithm. For example, utf8 and gbk have an intersection in the first two bytes. When a utf8 string is decoded with gbk encoding, the error occurs starting from the third byte (the first two bytes can also correspond to a Chinese character in the gbk encoding range). For example:

a = "你"              # utf8编码:&#39;\xe4\xbd\xa0&#39;
c = unicode(a[:2],&#39;gbk&#39;)  # 正常返回
c = unicode(a, &#39;gbk&#39;)    # UnicodeDecodeError 。错误发生在第三个字节
Copy after login

So for this situation, the following improvements have been made:

import codec

def cjk_replace(e):
  if not isinstance(e, UnicodeDecodeError):
    raise TypeError("invalid exception type %s" e)

  src = e.encoding
  if src in (&#39;gbk&#39;,&#39;gb18030&#39;, &#39;big5&#39;):
    beg = e.start - 2
    if beg >= 0:
      try:
        return unicode(e.object[beg:e.end], &#39;utf8&#39;), e.end + 1
      except:
        pass

  if exc.end + 1 > len(exc.object):
    raise TypeError(&#39;unknown codec ,the object too short!&#39;)
  ch1 = ord(exc.object[exc.start:exc.end])
  newpos = exc.end + 1
  ch2 = ord(exc.object[exc.start + 1:newpos])
  sk = exc.object[exc.start:newpos]

  if src != &#39;gbk&#39; and 0x81<=ch1<=0xFE and (0x40<=ch2<=0x7E or 0x7E<=ch2<=0xFE): # GBK
    return (unicode(sk,&#39;cp936&#39;), newpos)
  if src != &#39;big5&#39; and 0x81<=ch1<=0xFE and (0x40<=ch2<=0x7E or 0xA1<=ch2<=0xFE): # BIG5
    return (unicode(sk,&#39;big5&#39;), newpos)
  raise TypeError(&#39;unknown codec !&#39;)

codecs.register_error("cjk_replace", cjk_replace)
Copy after login

Of course, This logic is actually not rigorous enough. Although it is a bit realistic to deal with this abnormality of mixed encoding. But since python provides such capabilities, everyone can discuss together, how can we do better?

The above is the detailed content of python black magic encoding conversion method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles