Use python3 to randomly generate Chinese characters,

Source: Internet
Author: User

Use python3 to randomly generate Chinese characters,

Preface

The runtime environment is in Python3.6 and there are many Python2 solutions on the Internet. If you want to learn how to implement python2, you can refer to this article: http://www.bkjia.com/article/34884.htm.

Method 1: Unicode code

In unicode codes, the range of Chinese characters is (0x4E00, 9FBF)

import randomdef Unicode(): val = random.randint(0x4e00, 0x9fbf) return chr(val)

This method is relatively simple, but there is a small problem. The unicode Code contains more than 20 thousand Chinese characters, including many uncommon traditional Chinese characters.

Method 2: GBK2312

The gbk2312 character encoding uses a combination of two bytes. The first byte ranges from 0xB0-0xF7, and the second byte ranges from 0xA1-0xFE.
For more information about GBK2312 encoding, see GBK2312 encoding.

import randomdef GBK2312(): head = random.randint(0xb0, 0xf7) body = random.randint(0xa1, 0xfe) val = f'{head:x}{body:x}' str = bytes.fromhex(val).decode('gb2312') return str

GBK2312 contains more than frequently-used Chinese characters.

The following describes in detail the encode and decode functions of basic Python knowledge. The following is a brief introduction to the above implementation method, which has some reference value, let's take a look.

Ptyhon encode and decode Functions

In Python2.x, the basic encoding type is unicode encoding. Only Python 3.x is converted to a unicode-Based String.
So we will encounter various Encoding Problems in the study of Python2.x. The encode and decode functions are good tools to help us solve such problems.
The following procedures have detailed introductions and examples !~

In Python2.x:

U = u'china' # displays the specified unicode type object u str = u. encode ('gb2312') # encode the unicode image in gb2312 encoding str1 = u. encode ('gbk') # encode unicode objects using gbk encoding str2 = u. encode ('utf-8') # encode unicode objects with UTF-8 encoding u1 = str. decode ('gb2312') # decodes the str string in gb2312 encoding to obtain unicode u2 = str. decode ('utf-8') # If str is decoded using UTF-8, the original unicode type cannot be restored.

In Python3.x:

U = 'China' # specifies the string type object u str = u. encode ('gb2312') # encode u with gb2312 encoding to obtain the bytes type object str u1 = str. decode ('gb2312') # decodes the string 'str' encoded in gb2312 to obtain the string type object u1 u2 = str. decode ('utf-8') # If str is decoded with UTF-8, the original string content cannot be restored.

However, when reading and writing files, we should pay attention to the file format to determine how to operate them. By default, the txt files are UTF-8. Of course, you can also set your own encoding format.

We recommend that you convert the encoding format to UTF-8 when operating the txt file !~

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

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.