Developing python in the python console with a pycharm program has code that contains the output of chinese, and how to solve the problem similar

107 0

Like a problem similar to such a problem, it isn't known that the ~ is a problem, or a unicode method is required to convert.

Development environment is a version ~ of 1. 5. 2 for mac os x parking.

3 Answers

110 5

Python 2 code unified solution:

  • File encoding is # 8, while adding - *: utf-8 - * to the header.
  • All strings in the file are u u, e. G.".
  • Keep in mind that python is in a unicode store with unicode storage in the input ( crawl some utf pages ), output ( output to a web page ).
  • All inputs need to be encoded first, into unicode, and the output to be the desired encoding when output ( the output of the mac os x linux is utf, and the output of windows
  • There are many other older solutions, such as gbk, such as code without unicode, and so on. .

Update, from http://www. Newsmth. net/nForum/article..

  • Code for the source code: 8 specifies encoding and saves the file as utf.
  • In order to perform unnecessary errors as the main module, the n is used uniformly
  • If you need to embed characters in the source code, it's recommended that you use the u prefix as a unicode string so that you can do no coding
  • Otherwise, if the console encoding doesn't match the source code encoding, it must be explicitly converted through the locale. Getdefaultlocale ( ) to get the system language and encoding.
  • It's best to convert the file path to sys. Getfilesystemencoding ( ) encoding.
  • A unicode string must be converted to a byte encoding when writing to a file
  • Json default support for 8 encoding
  • As far as possible, utf 8 and unicode, except for the file path and write files previously mentioned
66 0
76 4