Python newbie (ofcourse) having some problems with decoding a URI-encoded string.
My code:
#/usr/bin/env python
# -*- coding: utf-8 -*-
# Encoding: UTF-8
...
import urllib
...
secondTag = urllib.unquote(secondTag).decode('utf8')
...
When
secondTag = "flashvars=%7B%22video%22%3A%7B%22videoReferences%22%3A%5B%7B%22url%22%3A%22http%3A%2F%2Fsvtplay6a-f.akamaihd.net%2Fz%2Fse%2Fopen%2F20160405%2F1114066-002A%2FPG-1114066-002A-AFFARENRAMEL-01_%2C988%2C240%2C348%2C456%2C636%2C1680%2C2796%2C.mp4.csmil%2Fmanifest.f4m%22%2C%22playerType%22%3A%22flash%22%7D%5D%2C%22subtitleReferences%22%3A%5B%7B%22url%22%3A%22http%3A%2F%2Fmedia.svt.se%2Fdownload%2Fmcc%2Ftest%2Fcore-prd%2FSUB-1114066-002A-AFFARENRAMEL%2FSUB-1114066-002A-AFFARENRAMEL.wsrt%22%7D%5D%2C%22position%22%3A0%7D%2C%22statistics%22%3A%7B%22client%22%3A%22nojs%22%2C%22mmsClientNr%22%3A%221001001%22%2C%22programId%22%3A%221114066-002A%22%2C%22statisticsUrl%22%3A%22%2F%2Fld.svt.se%2Fsvt%2Fsvt%2Fs%3Fnojs.Aff%C3%A4ren%20Ramel.Avsnitt%202%22%2C%22title%22%3A%22Avsnitt%202%22%2C%22folderStructure%22%3A%22Aff%C3%A4ren%20Ramel%22%7D%2C%22context%22%3A%7B%7D%7D"
Result is:
File "/home/mythtv/bin/pyPirateDownloader/svtPlay.py", line 70, in checkSecondSvtPage
secondTag = urllib.unquote(secondTag).decode('utf8')
File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 509-510: ordinal not in range(128)
However, when i run the same in python console I get the expected result:
>>> import urllib
>>> secondTag = "flashvars=%7B%22video%22%3A%7B%22videoReferences%22%3A%5B%7B%22url%22%3A%22http%3A%2F%2Fsvtplay6a-f.akamaihd.net%2Fz%2Fse%2Fopen%2F20160405%2F1114066-002A%2FPG-1114066-002A-AFFARENRAMEL-01_%2C988%2C240%2C348%2C456%2C636%2C1680%2C2796%2C.mp4.csmil%2Fmanifest.f4m%22%2C%22playerType%22%3A%22flash%22%7D%5D%2C%22subtitleReferences%22%3A%5B%7B%22url%22%3A%22http%3A%2F%2Fmedia.svt.se%2Fdownload%2Fmcc%2Ftest%2Fcore-prd%2FSUB-1114066-002A-AFFARENRAMEL%2FSUB-1114066-002A-AFFARENRAMEL.wsrt%22%7D%5D%2C%22position%22%3A0%7D%2C%22statistics%22%3A%7B%22client%22%3A%22nojs%22%2C%22mmsClientNr%22%3A%221001001%22%2C%22programId%22%3A%221114066-002A%22%2C%22statisticsUrl%22%3A%22%2F%2Fld.svt.se%2Fsvt%2Fsvt%2Fs%3Fnojs.Aff%C3%A4ren%20Ramel.Avsnitt%202%22%2C%22title%22%3A%22Avsnitt%202%22%2C%22folderStructure%22%3A%22Aff%C3%A4ren%20Ramel%22%7D%2C%22context%22%3A%7B%7D%7D"
>>> secondTag = urllib.unquote(secondTag).decode('utf8')
>>> print secondTag
flashvars={"video":{"videoReferences":[{"url":"http://svtplay6a-f.akamaihd.net/z/se/open/20160405/1114066-002A/PG-1114066-002A-AFFARENRAMEL-01_,988,240,348,456,636,1680,2796,.mp4.csmil/manifest.f4m","playerType":"flash"}],"subtitleReferences":[{"url":"http://media.svt.se/download/mcc/test/core-prd/SUB-1114066-002A-AFFARENRAMEL/SUB-1114066-002A-AFFARENRAMEL.wsrt"}],"position":0},"statistics":{"client":"nojs","mmsClientNr":"1001001","programId":"1114066-002A","statisticsUrl":"//ld.svt.se/svt/svt/s?nojs.Affären Ramel.Avsnitt 2","title":"Avsnitt 2","folderStructure":"Affären Ramel"},"context":{}}
Ofcourse this is some encoding problem, and I guess this has something to do with the 'ä' characters, as this problem doesn't occur when no swedish characters are present, but I just don't know why and how to fix it.
Someone able to explain and perhaps help with this?
Thanks /jon
Note that it is a UnicodeEncodeError: this isn't it failing to decode it, this is it failing to encode it!
Because Python 2 automatically converts between str
and unicode
, you can get encode errors if you try and decode a unicode string.
This is likely what is happening here: I presume in the file, secondTag
is a unicode
object: urllib.unquote
will then return a unicode
object, so when you try to decode it it first tries to encode it to a str
object so it can decode it, using the default ascii
encoding, which fails.
There's no particularly elegant way to handle this. Probably the most elegant way is urllib.unquote(secondTag.encode('utf8')).decode('utf8')
. If you want to handle the case where it is a str
already, you can easily enough add if isinstance(secondTag, unicode) else secondTag
.