Mom no longer worries about my coding. Integration of Chinese encoding, windows, django, python, java, html, djangopython

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.