Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Chinese problem of Jython

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to solve the Jython Chinese problem". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to solve the Jython Chinese problem".

Jython Chinese problem: garbled codes will be encountered when outputting Chinese

Some Jython learners reported that they would encounter Chinese garbled codes when using Jython to output Chinese characters. In view of the Jython Chinese problem, you need to have a declaration of the encoding format on the * * line. You can refer to the coding declaration in Python:

The encoding and decoding in Python is the mutual transformation of unicode and str. The encoding is unicode-> str; conversely, decoding is str-> unicode.

The remaining problem is to determine when to encode or decode, such as some libraries are unicode versions, so we have to consider encoding the return value of these library functions to the appropriate type when transferring or writing to a file.

The "encoding instruction" at the beginning of the file is the statement #-*-coding:-* -. Python default script files are all ANSCII encoded, so use the Encoding instruction to correct when there are characters in the file that are not in the ANSCII encoding range.

With regard to sys.defaultencoding, this is used when decoding does not explicitly specify the decoding method. For example, I have the following code:

#! / usr/bin/env python #-*-coding: utf-8-*-s = 'Chinese' # Note that the str here is of type str, not unicode s.encode ('gb18030')

This code re-encodes s into gb18030 format, that is, the conversion of unicode-> str. Because s itself is of type str, Python automatically decodes s into unicode and then encodes it into gb18030. Because the decoding is performed automatically by python, we do not specify the decoding method, and python will decode it in the way specified by sys.defaultencoding. In many cases, sys.defaultencoding is ANSCII, and if s is not of this type, an error will occur.

In the above case, my sys.defaultencoding is anscii, and the encoding method of s is the same as that of the file, which is utf8, so there is an error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position

0: ordinal not in range

In this case, we have two ways to correct the error:

One is to clearly indicate the coding mode of s.

#! / usr/bin/env python #-*-coding: utf-8-*-s = 'Chinese' s.decode ('utf-8'). Encode (' gb18030')

The second is to change the encoding method of sys.defaultencoding to file.

#! / usr/bin/env python #-*-coding: utf-8-*-import sys reload (sys) # Python2.5 will delete the sys.setdefaultencoding method after initialization. We need to reload sys.setdefaultencoding ('utf-8') str =' Chinese 'str.encode (' gb18030')

This should solve the problem of garbled codes in Jython.

The above is all the contents of the article "how to solve the Jython Chinese problem". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report