我有这个字符串:
sig=45C482D2486105B02211ED4A0E3163A9F7095E81.4DDB3B3A13C77FE508DCFB7C6CC68957096A406C\u0026type=video%2F3gpp%3B+codecs%3D%22mp4v.20.3%2C+mp4a.40.2%22\u0026quality=small\u
0026itag=17\u0026url=http%3A%2F%2Fr6---sn-cx5h-itql.c.youtube.com%2Fvideoplayback%3Fsource%3Dyoutube%26mt%3D1367776467%26expire%3D1367797699%26itag%3D17%26factor%3D1.25%2
6upn%3DpkX9erXUHx4%26cp%3DU0hVTFdUVV9OU0NONV9PTllHOnhGdTVLUThqUWJW%26key%3Dyt1%26id%3Dab9b0e2f311eaf00%26mv%3Dm%26newshard%3Dyes%26ms%3Dau%26ip%3D49.205.30.138%26sparams%
3Dalgorithm%252Cburst%252Ccp%252Cfactor%252Cid%252Cip%252Cipbits%252Citag%252Csource%252Cupn%252Cexpire%26burst%3D40%26algorithm%3Dthrottle-factor%26ipbits%3D8%26fexp%3D9
17000%252C919366%252C916626%252C902533%252C932000%252C932004%252C906383%252C904479%252C901208%252C925714%252C929119%252C931202%252C900821%252C900823%252C912518%252C911416
%252C930807%252C919373%252C906836%252C926403%252C900824%252C912711%252C929606%252C910075%26sver%3D3\u0026fallback_host=tc.v19.cache2.c.youtube.com
正如您所看到的,它包含两种形式:
%3
,%2F
等。\u0026
我需要将它们转换为unicode字符表示。我正在使用Python 3.3.1
,而urllib.parse.unquote(s)
仅将%xx
转换为其unicode字符表示形式。但是,它不会将\uxxxx
转换为其unicode字符表示形式。例如,\u0026
应转换为&
。
如何转换它们?
答案 0 :(得分:4)
两个选项:
选择将其解释为JSON;该格式使用相同的转义码。输入确实需要在它周围加上引号才能看作字符串。
编码为latin 1(保留字节),然后用unicode_escape
编解码器解码:
>>> urllib.parse.unquote(sig).encode('latin1').decode('unicode_escape')
'45C482D2486105B02211ED4A0E3163A9F7095E81.4DDB3B3A13C77FE508DCFB7C6CC68957096A406C&type=video/3gpp;+codecs="mp4v.20.3,+mp4a.40.2"&quality=small&itag=17&url=http://r6---sn-cx5h-itql.c.youtube.com/videoplayback?source=youtube&mt=1367776467&expire=1367797699&itag=17&factor=1.25&upn=pkX9erXUHx4&cp=U0hVTFdUVV9OU0NONV9PTllHOnhGdTVLUThqUWJW&key=yt1&id=ab9b0e2f311eaf00&mv=m&newshard=yes&ms=au&ip=49.205.30.138&sparams=algorithm%2Cburst%2Ccp%2Cfactor%2Cid%2Cip%2Cipbits%2Citag%2Csource%2Cupn%2Cexpire&burst=40&algorithm=throttle-factor&ipbits=8&fexp=917000%2C919366%2C916626%2C902533%2C932000%2C932004%2C906383%2C904479%2C901208%2C925714%2C929119%2C931202%2C900821%2C900823%2C912518%2C911416%2C930807%2C919373%2C906836%2C926403%2C900824%2C912711%2C929606%2C910075&sver=3&fallback_host=tc.v19.cache2.c.youtube.com'
这解释了\u
转义码,就像Python在Python源代码中读取字符串文字时所做的那样。
答案 1 :(得分:2)
如果我猜对了,这或多或少是一个URL。 '%xx'编码允许字符集之外的单个字节。 '\ uxxxx'对Unicode代码点进行编码。我认为URL将Unicode字符编码为UTF-8然后将允许的字符集外的字节编码为'%xx'(影响所有多字节UTF-8序列)是正常的。这令人惊讶的是,已经有'%xx'编码的字节,因为翻译Unicode代码点会使转换不可逆转。
确保您有测试并且可以验证实际结果,因为这似乎是不安全的。至少 I 并不完全理解这里的要求。