2

How to do the following conversions in Python?

"杭州" to "%BA%BC%D6%DD"
"大连" to "%B4%F3%C1%AC"
unutbu
887k197 gold badges1.9k silver badges1.7k bronze badges
asked Jun 26, 2011 at 1:13

2 Answers 2

3

To convert u'杭州' to '%BA%BC%D6%DD':

In [24]: ''.join('%{0:X}'.format(ord(c)) for c in u"杭州".encode('gbk'))
Out[24]: '%BA%BC%D6%DD'
In [25]: ''.join('%{0:X}'.format(ord(c)) for c in u"大连".encode('gbk'))
Out[25]: '%B4%F3%C1%AC'

To convert '%BA%BC%D6%DD' to u'杭州':

In [54]: import binascii 
In [55]: print(binascii.unhexlify(''.join('%BA%BC%D6%DD'.split('%'))).decode('gbk'))
杭州
answered Jun 26, 2011 at 1:23
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer. it works. could you please teach me how to convert strings like '%BA%BC%D6%DD' back to u"杭州"?
@jack: I've added a way to convert '%BA%BC%D6%DD' back to u"杭州". Note however that Dietrich Epp provided a better answer (using urllib.parse.quote and/or urllib.parse.unquote) if your Python version is modern enough (e.g. Python3).
3

You'll need to give us more information.

def encode(s):
 if s == "杭州":
 return "%BA%BC%D6%DD"
 if s == "大连":
 return "%B4%F3%C1%AC"
 raise ValueError

You need to tell us the encoding you're using, for one thing.

import urllib.parse
def encode(s):
 return urllib.parse.quote(s.encode('gbk'))
answered Jun 26, 2011 at 1:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.