0

How would I urlencode the following? What I am currently doing throws a KeyError.

episode_title = 'ザ・ロック(日本語吹替版)'
try:
    qs = urllib.quote(episode_title)
except:
    qs = urllib.quote(episode_title.encode('utf8'))

Traceback (most recent call last):
  KeyError: u'\u30b6'
David542
  • 104,438
  • 178
  • 489
  • 842
  • what python version are you on? – Anzel Mar 06 '15 at 03:18
  • 1
    See http://stackoverflow.com/questions/912811/what-is-the-proper-way-to-url-encode-unicode-characters -- TL;DR: unconditionally urllib.urlencode the utf-8 encoding of your Unicode string. – Alex Martelli Mar 06 '15 at 03:22
  • If you are doing this using Python2, doing `episode_title = u'ザ・ロック(日本語吹替版)'` works with the encoded result of that `episode_title` unicode string (i.e. the statement in the exception block). – metatoaster Mar 06 '15 at 03:26
  • try adding unicode to the string: `episode_title = u'ザ・ロック(日本語吹替版)'`, and do: `qs = urllib.quote(episode_title.encode('utf8'))` and see if this works – Anzel Mar 06 '15 at 03:27

1 Answers1

2

Maybe add an decode('utf-8') to your string.

import urllib
episode_title = "ザ・ロック(日本語吹替版)".decode('utf-8')
try:
    qs = urllib.quote(episode_title)
except:
    qs = urllib.quote(episode_title.encode('utf8'))
print qs

Result:

%E3%82%B6%E3%83%BB%E3%83%AD%E3%83%83%E3%82%AF%EF%BC%88%E6%97%A5%E6%9C%AC%E8%AA%9E%E5%90%B9%E6%9B%BF%E7%89%88%29

PS: I use iPython, and your code works fine. Maybe you can switch to another Editor.

Aaron
  • 2,383
  • 3
  • 22
  • 53