2014-08-29 77 views
2

OK等等...ast.literal_eval莫名其妙地扔的UnicodeDecodeError

  • 一個Unicode字符串被編碼爲Python 2.x的字符串(實際上,字節序列)
  • 一個Python 2.x的字符串被解碼爲Unicode字符串

Python UnicodeDecodeError - Am I misunderstanding encode?

我已經得到了這條巨蟒2.7代碼

try: 
    print '***' 
    print type(relationsline) 
    relationsline = relationsline.decode("ascii", "ignore") 
    print type(relationsline) 
    relationsline = relationsline.encode("ascii", "ignore") 
    print type(relationsline) 
    relations = ast.literal_eval(relationsline) 
except ValueError: 
    return 
except UnicodeDecodeError: 
    return 

在最後一行代碼以上有時會拋出

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

我認爲這將(1)開始的字符串與一些(未知)編碼(2)它解碼成一個unicode類型,表示一個字符串,它使用ascii編碼設置unicode字符,同時忽略所有不能使用ascii編碼的字符(3)將unicode類型編碼爲具有ascii編碼的字符串,忽略所有無法表示的字符ASCII。

以下是完整的堆棧跟蹤:

Traceback (most recent call last): 
    File "outputprocessor.py", line 69, in <module> 
    getPersonRelations(lines, fname) 
    File "outputprocessor.py", line 41, in getPersonRelations 
    relations = ast.literal_eval(relationsline) 
    File "/usr/lib/python2.7/ast.py", line 49, in literal_eval 
    node_or_string = parse(node_or_string, mode='eval') 
    File "/usr/lib/python2.7/ast.py", line 37, in parse 
    return compile(source, filename, mode, PyCF_ONLY_AST) 
    File "<unknown>", line 1 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 341: ordinal not in range(128) 
        ^
SyntaxError: invalid syntax 

但是,這顯然是錯誤的地方。更令人困惑的是,UnicodeDecodeError沒有捕獲UnicodeDecodeError。我錯過了什麼?也許這是問題? http://bugs.python.org/issue22221

+0

你的ascii文本不是。 strict ascii是一個7位字符集(0x00 - > 0x7F),並且你有一個大於0x7F的字符,這意味着它不是ascii。也許它擴展ascii,iso8859-1,或其他。但它不是「ascii」。 – 2014-08-29 16:31:20

+0

如果你能弄清一個能持續拋出錯誤的例子,這將更容易調試。建議使用[MCVE](http://stackoverflow.com/help/mcve):一個最小的獨立代碼示例,運行時會產生您在運行時遇到的錯誤。 – user2357112 2014-08-29 16:35:52

+0

另外,請向我們展示異常的完整堆棧跟蹤。 – user2357112 2014-08-29 16:38:21

回答

1

仔細看看堆棧軌跡。這是投擲SyntaxError

您正在嘗試literal_eval字符串"UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 341: ordinal not in range(128)"。你可以編碼/解碼所有你想要的字符串,但ast不知道該如何處理它 - 這顯然不是一個有效的python字面量。

參見:

>>> import ast 
>>> ast.literal_eval('''UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 341: ordinal not in range(128)''') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.7/ast.py", line 49, in literal_eval 
    node_or_string = parse(node_or_string, mode='eval') 
    File "/usr/lib/python2.7/ast.py", line 37, in parse 
    return compile(source, filename, mode, PyCF_ONLY_AST) 
    File "<unknown>", line 1 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 341: ordinal not in range(128) 
        ^
SyntaxError: invalid syntax 

我想看看無論是通過這些字符串到您的函數的源代碼,它產生一些虛假的輸入。

0

您正在嘗試literal_eval從傳入的字符串中追溯relationsline = relationsline.encode("ascii", "ignore")

您需要將您的literal_eval支票移動到它自己的try/except中,或在原始嘗試區塊中捕獲異常或以某種方式過濾輸入。

+0

咦?在他的代碼中,他將traceback分配給'relationshipline'?這不是如何例外工作... – roippi 2014-08-29 17:20:49

+0

@roippi,我的意思是從傳入的字符串。 – 2014-08-29 18:10:27