Source: http://blog.chenlb.com/2010/01/python-use-utf-8.html
Generally, I like to use UTF-8 encoding. How can I use it in Python?
1. Use UTF-8 In the Python source code file. Generally, an error is reported as follows:
File "F: \ workspace \ PSH \ SRC \ test. py", line 2
Syntaxerror: Non-ASCII character '\ xe4' in file F: \ workspace \ PSH \ SRC \ test. py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Test. py content:
- Print "Hello"
If you want to run normally, add an encoding comment before the test. py file, for example:
- #! /Usr/bin/python2.6
-
- #-*-Coding: UTF-8 -*-
- Print "Hello"
2,
How does Python do the URL encode UTF-8?
How does one convert Windows command line parameters to UTF-8?
Code:
- #-*-Coding: UTF-8 -*-
-
- ImportUrllib
- ImportSys
-
- If _ name _ = '_ main _' :
- IfLen (SYS. argv)>1:
-
- STR = SYS. argv [1]
- STR = Unicode (STR,'Gbk')
-
- Else:
- STR ="Chinese"
-
- PrintStr
-
- Params = {}
- Params ['Name'] = Str. encode (UTF-8")
-
- PrintUrllib. urlencode (Params)
In python, Unicode is used internally.
Since Windows command lines are encoded in GBK format, you can convert them to Unicode (the third eight rows ).
To convert the URL to encode, convert STR to UTF-8.
Default output result:
Chinese
Name = % E4 % B8 % ad % E6 % 96% 87
It is easy to write a Python script for small tasks. For example, to obtain SOLR data, the solr url encoding is UTF-8.
Reference: http://evanjones.ca/python-utf8.html