P
P
pcdesign2015-02-03 20:16:50
Perl
pcdesign, 2015-02-03 20:16:50

What is the equivalent of the Perl module (URI::Escape::JavaScript) in Python3?

Here is the code from Perl:

perl -E 'use URI::Escape::JavaScript qw/escape unescape/; use Encode; $a = "яблоко"; Encode::_utf8_on($a);  say escape($a);'

Result:
%u044F%u0431%u043B%u043E%u043A%u043E
How to get the same in Python3?
In the forehead, what happens:
from urllib import quote

query = 'яблоко'
print(quote(query))

and the result:
%D1%8F%D0%B1%D0%BB%D0%BE%D0%BA%D0%BE
How to get a string one to one like in Perl?
How to turn "apple" into "%u044F%u0431%u043B%u043E%u043A%u043E" in Python?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2015-02-03
@pcdesign

On my knee I found this solution:

import json
import re

query = "яблоко"
esc = json.dumps(query).upper()
p = re.compile(r"\\U", re.I)
esc = p.sub('%u', esc)
print(esc)

Result:
But somehow I don't like it all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question