0

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?

1 Answer 1

4

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).

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

Comments

Your Answer

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.