Python 3.6 - Can't convert Unicode in a string

拜拜、爱过 提交于 2020-04-14 07:57:08

问题


I am doing some scraping work with Python 3.6 and retrieved some URLs in strings following this format:

someURL = 'http:\u002F\u002Fsomewebsite.com\u002Fsomefile.jpg'

I have been trying to convert the Unicode backslash (\u002F) in those strings in order to use the URLs (using regex methods, encode() on the strings, etc.), to no avail. The string still keeps the Unicode backslash and, if I pass it on to Requests' get(), for example, I am greeted by the following error message:

InvalidURL: Failed to parse: http:\u002F\u002Fsomewebsite.com\u002Fsomefile.jpg"

I searched for solutions in this forum and and others, but can't seem to put the finger on it. I am sure it is simple...


回答1:


Use codecs.decode with the encoding named 'unicode-escape':

import codecs
print(codecs.decode(someURL, 'unicode-escape'))
# prints 'http://somewebsite.com/somefile.jpg'


来源:https://stackoverflow.com/questions/45527763/python-3-6-cant-convert-unicode-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!