-3

当我对字符串进行编码时。它给出了这样的错误UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 304: ordinal not in range(128)

value = ""
Do not delete this article, there is ad-supported access to 500 U.S. dollars reward, activity time to 2010/12/31 the end of this period we will have staff members from time to time to visit, hope you have the opportunity to receive prizes!<br /><br /><a href="http://www.fairytale.com.tw" rel="nofollow">\xe9\x97\x9c\xe9\x8d\xb5\xe5\xad\x97\xe8\xa1\x8c\xe9\x8a\xb7</a> <a href="http://www.fairytale.com.tw" rel="nofollow">\xe7\xb6\xb2\xe8\xb7\xaf\xe8\xa1\x8c\xe9\x8a\xb7</a> <a href="http://www.fairytale.com.tw" rel="nofollow">\xe8\xbb\x9f\xe9\xab\x94\xe8\xa1\x8c\xe9\x8a\xb7</a> <a href="http://www.shin-long.com" rel="nofollow">\xe6\x90\xac\xe5\xae\xb6</a> <a href="http://www.proteus88.com.tw" rel="nofollow">\xe6\xb4\xbb\xe6\xb0\xb4\xe6\xa9\x9f</a> <a href="http://www.proteus88.com.tw" rel="nofollow">\xe6\xbf\xbe\xe6\xb0\xb4\xe5\x99\xa8</a> <a href="http://www.proteus88.com.tw" rel="nofollow">\xe6\xb7\xa8\xe6\xb0\xb4\xe5\x99\xa8</a> <a href="http://1069.gv520.com" rel="nofollow">1069</a> <a href="http://www.lv-3a.com" rel="nofollow">GUCCI</a> <a href="http://www.lv-3a.com" rel="nofollow">GUCCI\xe5\x8c\x85</a> <a href="http://www.lv-3a.com" rel="nofollow">GUCCI\xe5\x8c\x85\xe5\x8c\x85</a> <a href="http://www.lv-3a.com" rel="nofollow">LV\xe5\x8c\x85</a> <a href="http://www.lv-3a.com" rel="nofollow">LV</a> <a href="http://www.lv-3a.com" rel="nofollow">LV\xe5\x8c\x85\xe5\x8c\x85</a> <a href="http://www.footpure.com.tw" rel="nofollow">\xe9\x99\xa4\xe8\x85\xb3\xe8\x87\xad</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe8\x87\xaa\xe6\x85\xb0\xe5\x99\xa8</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe6\x83\x85\xe8\xb6\xa3\xe7\x94\xa8\xe5\x93\x81</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe6\x83\x85\xe8\xb6\xa3\xe5\x95\x86\xe5\x93\x81</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe8\xb7\xb3\xe8\x9b\x8b</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe6\x8c\x89\xe6\x91\xa9\xe6\xa3\x92</a> <a href="http://www.chichiau.com.tw" rel="nofollow">\xe9\xab\x98\xe9\x9b\x84\xe7\x9b\xa3\xe8\xa6\x96\xe5\x99\xa8</a> <a href="http://www.chichiau.com.tw" rel="nofollow">\xe7\x9b\xa3\xe8\xa6\x96\xe5\x99\xa8</a> <a href="http://www.chichiau.com.tw" rel="nofollow">\xe7\x9b\xa3\xe8\xa6\x96\xe5\x99\xa8\xe6\x9d\x90</a> <a href="http://www.chichiau.com.tw" rel="nofollow">\xe7\x9b\xa3\xe8\xa6\x96\xe6\x94\x9d\xe5\xbd\xb1</a> <a href="http://www.vemma168.com" rel="nofollow">\xe7\xb6\xb2\xe8\xb7\xaf\xe5\x89\xb5\xe6\xa5\xad</a> <a href="http://okpawn.com.tw" rel="nofollow">\xe7\x95\xb6\xe9\x8b\xaa</a> <a href="http://okpawn.com.tw" rel="nofollow">\xe7\x95\xb6\xe8\x88\x96</a> <a href="http://okpawn.com.tw" rel="nofollow">\xe6\xb5\x81\xe7\x95\xb6\xe5\x93\x81</a> <a href="http://okpawn.com.tw" rel="nofollow">\xe7\x89\xb9\xe8\xb3\xa3\xe6\x9c\x83</a> <a href="http://www.evandesign.com.tw" rel="nofollow">\xe5\x9c\x98\xe9\xab\x94\xe6\x9c\x8d</a> <a href="http://966.tw" rel="nofollow">\xe5\x85\x8d\xe8\xb2\xbb\xe5\xbb\xa3\xe5\x91\x8a</a> <a href="http://www.23024623.com" rel="nofollow">\xe4\xbd\x9b\xe5\x85\xb7</a> <a href="http://www.0936920085.com" rel="nofollow">\xe5\x8a\xa0\xe7\x9b\x9f</a> <a href="http://www.0289859147.com" rel="nofollow">\xe8\x87\xad\xe8\x87\xad\xe9\x8d\x8b</a> <a href="http://www.0289859147.com" rel="nofollow">\xe5\x8a\xa0\xe7\x9b\x9f</a> <a href="http://www.0289859147.com" rel="nofollow">\xe7\x81\xab\xe9\x8d\x8b</a> <a href="http://www.katehuang16899.com" rel="nofollow">\xe6\x9c\x9f\xe8\xb2\xa8</a> <a href="http://www.naza.com.tw" rel="nofollow">\xe5\xa9\x9a\xe7\xb4\x97</a> <a href="http://www.weige.tw" rel="nofollow">\xe6\x83\x85\xe8\xb6\xa3\xe7\x94\xa8\xe5\x93\x81</a> <a href="http://www.weige.tw" rel="nofollow">\xe6\x83\x85\xe8\xb6\xa3</a> <a href="http://www.007uic.com.tw" rel="nofollow">\xe5\xa5\xb3\xe4\xba\xba\xe5\xbe\xb5\xe4\xbf\xa1</a> <a href="http://www.tpedetect.org.tw" rel="nofollow">\xe5\xa5\xb3\xe4\xba\xba\xe5\xbe\xb5\xe4\xbf\xa1</a> <a href="http://www.kaodetect.org.tw" rel="nofollow">\xe5\xa5\xb3\xe4\xba\xba\xe5\xbe\xb5\xe4\xbf\xa1</a> <a href="http://www.chinese007.org.tw" rel="nofollow">\xe5\xa5\xb3\xe4\xba\xba\xe5\xbe\xb5\xe4\xbf\xa1</a> <a href="http://www.worldclass.com.tw" rel="nofollow">\xe5\xa5\xb3\xe4\xba\xba\xe5\xbe\xb5\xe4\xbf\xa1</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe6\xb8\x9b\xe8\x82\xa5</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe8\xb1\x90\xe8\x83\xb8</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe6\x95\xb4\xe5\xbd\xa2</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe9\x9b\xbb\xe6\xb3\xa2\xe6\x8b\x89\xe7\x9a\xae</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe9\x9b\xb7\xe5\xb0\x84\xe6\xba\xb6\xe8\x84\x82</a> <a href="http://www.li-xin.com.tw" rel="nofollow">\xe9\xa3\x9b\xe6\xa2\xad\xe9\x9b\xb7\xe5\xb0\x84</a> <a href="http://shop.hi5.tv" rel="nofollow">\xe7\xa7\x81\xe5\xaf\x86\xe8\x99\x95\xe4\xbf\x9d\xe9\xa4\x8a</a> <a href="http://www.seo-104.com" rel="nofollow">seo</a> <a href="http://www.seo-104.com" rel="nofollow">104</a> <a href="http://www.seo-104.com" rel="nofollow">104\xe8\xab\x96\xe5\xa3\x87</a> <a href="http://www.twseo99.com" rel="nofollow">seo</a> <a href="http://www.twseo99.com" rel="nofollow">\xe8\xa1\x8c\xe9\x8a\xb7\xe8\xbb\x9f\xe9\xab\x94</a> <a href="http://www.vemma168.net" rel="nofollow">\xe5\x85\x8d\xe8\xb2\xbb\xe7\xb6\xb2\xe8\xb7\xaf\xe5\x89\xb5\xe6\xa5\xad</a> <a href="http://www.twvemma.com" rel="nofollow">\xe7\xb6\xb2\xe8\xb7\xaf\xe8\xb3\xba\xe9\x8c\xa2</a>
"""



In [2]: type(value)
Out[2]: str

In [3]: encoded_value = value.encode("utf-8")
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
/home/nyros/hs/1a9pinaxenv/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 encoded_value = value.encode("utf-8")

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 304: ordinal not in range(128)

In [4]: 
4

1 回答 1

1

这是我用你的value

>>> type(value)
<type 'str'>

>>> v = value.decode('utf-8')
>>> type(v)
<type 'unicode'>

如果您想知道您value是编码还是解码,您可以从错误消息中看到,UnicodeDecodeError如果您尝试encode使用已编码的字符串,则会引发 a。

那么如何尝试然后捕获异常:

try:
    value = value.encode('utf-8')
except UnicodeDecodeError:
    print('It is already a UTF-8 encoded string')
else:
    print('It may have been a UTF-8 encoded string')
于 2013-10-09T06:29:18.417 回答