Use python to output the Chinese Character Library, and use python to output the Chinese Character Library

Source: Internet
Author: User

Use python to output the Chinese Character Library, and use python to output the Chinese Character Library

Question 1: If we know that the Chinese character encoding range is 0x4E00 to 0x9FA5, how can we convert the hexadecimal code into a human-readable word?

Question 2: How can I write unicode-encoded words into a file? If open () is used directly, the UnicodeEncodeError message is displayed: 'ascii 'codec can't encode character U' \ u4e00' in position 0: ordinal not in range (128)


The answer to question 1 is unichr, and the answer to question 2 is codes.

The code below.

Import codecsstart, end = (0x4E00, 0x9FA5) with codecs. open ("chinese.txt", "wb", encoding = "UTF-8") as f: for codepoint in range (int (start), int (end): f. write (unichr (codepoint ))


Open the chinese.txt file, as shown below:


Python reading Chinese files, encoding is ANSI format, hope to be able to output one by one Chinese characters so want to convert into a UTF-8 Solution

Generally, the read string requires "I". decode ("ascii"). encode ("UTF-8 ")
If not, use the following code to change the encoding. I recently solved this problem.
Import sys
Reload (sys)
Sys. setdefaultencoding ('utf8 ')

Python output problems

I have encountered this problem. his dictionary is unordered, but you can control the order of KEY or value, that is, you write a list, that is, you want the order, which is the key, then write a loop for this list to retrieve the value in the dictionary.

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.