Apollo
Apollo

Reputation: 9054

Encode string for URL - Python

I have the following URL that needs to be encoded for a URL: This is currently the top headline on Reddit TIL Pimps wear lots of gold jewelry bought at pawn shops to “re-pawn” for bail money since cash is confiscated upon arrest but jewelry is not

I'm running into a problem since this string contains unicode characters, specifically the quotations.

I've tried urllib.quote_plus(message) but this raises the following exception:

Traceback (most recent call last):
  File "testProgram.py", line 44, in <module>
    main()                                      # Run
  File "testProgram.py", line 41, in main
    testProgram(headline)                                   # Make phone call
  File "testProgram.py", line 31, in testProgram
    urllib.quote_plus(message)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1293, in quote_plus
    s = quote(s, safe + ' ')
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1288, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u201c'

Anybody know why this is?

Upvotes: 0

Views: 266

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881487

If message is a Unicode string, then try:

urllib.quote_plus(message.encode('utf-8'))

utf-8 is, alas, not universally used in URLs (I don't think there is a universally accepted standard, alas), but it's quite prevalent thanks to its "universal" nature (every Unicode character can be represented in utf-8, which is not the case for many other popular encodings).

Upvotes: 4

Related Questions