2017-03-14 43 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 :) 
相关问题