Python 3 and Javascript escape transfer ensure data correct method and Chinese garbled solution

Source: Internet
Author: User
Tags url decode

A few days ago with the Python bottle framework to write a small Web program, in the Ajax interaction, the front end is the first to use Json.stringify to serialize the class, and then use the Escape () function to encode it to ensure that the transmission is correct.

And then basically with the jquery $.ajax should be possible, may be inexperienced, even after the encoded data is still difficult to handle in Python.

Then slowly thinking about a way, on the internet also found a similar way, so it is realized.

The basic ideas are as follows:

Escape (' Hello World abc '); // back to "%U4F60%U597D%U4E16%U754CABC"

After this string of strings was submitted to bottle, I decoded it using Python's urllib and found a problem.

>>> urllib.parse.unquote ('%u4f60%u597d%u4e16%u754cabc')'  %u4f60%u597d%u4e16%u754cabc'

What this string should look like or what it looks like, so the study of a moment, and then the idiot found that this is not a URL encoded characters, can not be solved with unquote.

We should use decode (' UTF-8 ')!

%uxxxx is the way JavaScript turns into Unicode. So we have to make it into the standard form of \uxxxx Unicode .

And Python unquote can only be URL decoding str, so this is Unicode encoding of Chinese characters can not be solved, then I must use Decode (' UTF-8 ');

But the received character is STR, there is no decode, only encode. And then we checked the manual and found a urllib.parse.unquote_to_bytes function that decodes the STR URL and returns a byte.

Right, that's it, and then, based on the returned byte, you can use decode for it.

So I wrote one:

 def   = value.replace ( " %u  " ,  " \\u   ") #   replace%uxxxx with \uxxxx to Utf-8 decode  Byts = urllib.parse.unquote_to_bytes (value) #   returned byte  Byts  = Byts.decode (' UTF-8 ')  # decode UTF-8 decoding can only unlock \uxxxx The Unicode standard form of   return  json.loads (Byts)  

And do the following tests:

Escape (' {' "value": [123, "Hello World abc"]} ')//"%7b%22value%22%3a%5b123%2c%22%u4f60%u597d%u4e16% U754cabc%22%5d%7d "

Python Shell:

>>> Load_json ('%7b%22value%22%3a%5b123%2c%22%u4f60%u597d%u4e16%u754cabc%22%5d%7d  '{'value' Hello World abc']}

Congratulations on the success of the test, it seems to have succeeded.

Summary:

In this case, the character will be encoded in Unicode, even if any. JavaScript uses escape (other lines) to encode characters UTF-8, although it is%uxxxx, but you can translate them into the standard form of \uxxxx.

And even if some particular wayward browser doesn't turn into%uxxxx, we'll just replace the%u, and it won't affect the actual characters.

Process:

Javascript Object, json.stringify (obj), Escape (json_str), automatic URL encoding for browsers (except wayward) Python urllib URL decode -%uxxxx Replacement \uxxxx, decode (' UTF-8 '), Json.load ();

 This is only a bit of experience, if there is any mistake or better, please correct, learn from it, will be thanked.

Python 3 and Javascript escape transfer ensure data correct method and Chinese garbled solution

Related Article

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.