1

I have a string which I encoded with python urllib2.quote(). The string in question is "Z%C3%BCrich", which was encoded from "Zürich". However, there seems to be a discrepancy between the way python handles this and the way JavaScript does.

In python:

>>> print urllib2.unquote("Z%C3%BCrich") Zürich 

In JavaScript:

> unescape("Z%C3%BCrich") 'Zürich' 

These are taking in the same input string, but producing very different output strings. What am I doing wrong?

Note: This post is not a duplicate of this post, in which the author had the opposite problem.

3
  • 1
    Try decodeURI("Z%C3%BCrich"). Commented Feb 12, 2016 at 18:02
  • 2
    Basically, urllib2.quote() is more like encodeURI() in JavaScript. And urllib2.unquote() is decodeURI() rahter than unescape(). You should always avoid escape() and unescape() function in JavaScript because they'll give you weird output...like you're getting now. Commented Feb 12, 2016 at 18:04
  • 2
    Great, didn't know that. I'm experienced in Python but rather new to JavaScript. I appreciate the swift response. Since you answered first, if you post that as an answer I'll accept yours. Commented Feb 12, 2016 at 18:05

2 Answers 2

2

Posting my comments as an answer.

You should use:

decodeURI("Z%C3%BCrich") 

Instead of unescape("Z%C3%BCrich") in JavaScript.

Basically, urllib2.quote() is more like encodeURI() in JavaScript. And urllib2.unquote() is decodeURI() rahter than unescape().

You should always avoid escape() and unescape() function in JavaScript because they'll give you weird output...like you're getting now.

Sign up to request clarification or add additional context in comments.

Comments

1

The Javascript function unescape() is deprecated, you should use decodeURI() instead. unescape() is giving you the utf-8 encoded form of your string rather than the unicode you want.

1 Comment

Thanks so much for the swift response, it works great.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.