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.