2017-03-14 64 views
0

我正在使用Python Markdown來解析下表。解析Python標記中的表時出現Unicode錯誤

Escape sequences | Character represented 
-----------------|-------------------------- 
\b  | Backspace 
\t  | Tab 
\f  | Form feed 
\n  | New line 
\r  | Carriage return 
\\  | Backslash 
\'  | Single quote 
\"  | Double quote 
\uNNNN | where NNNN is a unicode number, with this escape sequence you can print unicode characters 

這裏是我使用

html = markdown.markdown(str, extensions=['markdown.extensions.tables', 'markdown.extensions.fenced_code', 
              'markdown.extensions.toc', 'markdown.extensions.wikilinks']) 
print(html) 

的代碼,這裏是錯誤

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1000-1001: truncated \uXXXX escape 

回答

2

這裏的問題是,你的輸入字符串包含有special meaning反斜線符號。要讓它工作,輸入數據應該如下所示:

Escape sequences | Character represented 
-----------------|-------------------------- 
\\b  | Backspace 
\\t  | Tab 
\\f  | Form feed 
\\n  | New line 
\\r  | Carriage return 
\\\\  | Backslash 
\\'  | Single quote 
\\"  | Double quote 
\\uNNNN | where NNNN is a unicode number, with this escape sequence you can print unicode characters 

即反斜槓應該自行轉義。 達到此目的的愚蠢方式 - 可能只是在使用降價解析之前進行一些預處理:

str.replace('\\', '\\\\') # yes, here too :) 
相關問題