Mom no longer worries about my coding. Integration of Chinese encoding, windows, django, python, java, html, djangopython
Use django to write a small web application. This application calls a business-related jar package and displays the returned values of the jar package. In windows, the django framework is used to call the java jar package in python and output the content to html. It involves a bunch of coding problems.
The process is not garbled or garbled. Finally, we can solve the problem at the expert's guidance. It is hereby recorded. Mom no longer worries about my coding.
1. the python file header indicates the encoding format used by the file content #-*-coding: UTF-8 -*-
2. python sys encoding format. Reload (sys)
Sys. setdefaultencoding ("utf8") 3. locale. getpreferredencoding () 4. python can print the encoding formats of standard input and standard output. Print sys. getdefaultencoding ()
Print locale. getpreferredencoding ()
Print sys. stdin. encoding
Print sys. stdout. encoding
5. python encode encodes a string into a hexadecimal number. Decode is decoded into the Chinese character msg = "Liu This is a stream. Volume calibration text message, in order to keep him longer? Abcdefg. Why"
Print msg. encode ('utf-8') cmd = "java-Dfile. encoding = UTF-8-jar/netflowcorrect-0.1-jar-with-dependencies.jar % s" % msg. encode ('gbk ')
Print cmd. decode ('gbk'). encode ('utf8 ')
6. the command line in the window environment uses gbk encoding by default. If you use utf8 to encode a parameter, it will be decoded into gbk by default by the system, and it will be garbled (only a small part may be garbled. Most of the time is used to confirm this issue.
7. the java command can specify the default character encoding java-Dfile. encoding = UTF-8-jar/netflowcorrect-0.1-jar-with-dependencies.jar8 java print character encoding System. out. println ("Default Charset =" + Charset. defaultCharset (); System. setProperty ("file. encoding "," Latin-1 "); System. out. println ("file. encoding = "+ System. getProperty ("file. encoding "); System. out. println ("Default Charset =" + Charset. defaultCharset (); System. out. println ("Default Charset in Use =" + getdefacharcharset (); private static String getdefadefacharset () {OutputStreamWriter writer = new OutputStreamWriter (new ByteArrayOutputStream (); String enc = writer. getEncoding (); return enc;} 9 String encoding conversion log in java ("input 2:" + new String (args [index]. getBytes ("gbk"), "gbk"); log ("input 3:" + new String (args [index]. getBytes ("UTF-8"), "gbk"); log ("input 4:" + new String (args [index]. getBytes ("GB2312"), "gbk"); log ("input 5:" + new String (args [index]. getBytes ("cp936"), "gbk"); log ("Input 6:" + new String (args [index]. getBytes ("ISO-8859-1"), "gbk"); 10 java System. out Encoding
Private static void log (String msg) {try {PrintStream out = new PrintStream (System. out, true, "UTF-8"); out. println (msg);} catch (UnsupportedEncodingException e) {e. printStackTrace ();}}
11 encoding of html files <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
</Head>