[英]python string encoding unicode
我正在使用python 2.7,将“ä”等字符转换为“ ae”时遇到一些问题。
我正在使用以下方法检索网页的内容:
req = urllib2.Request(url + str(questionID))
response = urllib2.urlopen(req)
data = response.read()
之后,我正在做一些提取工作,这就是我的问题。
extractedStr = pageContent[start:end] // this string contains the "ä" !
extractedStr = extractedStr.decode("utf8") // here I get the error, tried it with encode aswell
extractedStr = extractedStr.replace(u"ä", "ae")
->'utf8'编解码器无法解码位置13的字节0xe4:无效的连续字节
但是:我的简单试验工作正常……:
someStr = "geräusch"
someStr = someStr.decode("utf8")
someStr = someStr.replace(u"ä", "ae")
我有种感觉,这与我尝试使用.decode()函数有关……我在多个位置尝试过,但没有成功:(
请改用.decode("latin-1")
。 那就是您要解码的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.