简体   繁体   English

SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置0-1的字节:格式错误的\\ N字符转义

[英]SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape

Apologies, I know there are similar threads, but I still haven't had any luck! 抱歉,我知道也有类似的话题,但是我还是没有运气! I'm brand new to Python & trying to interpret a code that a colleague wrote. 我是Python的新手,正在尝试解释一位同事编写的代码。

I keep getting the Unicodeescape error: 我不断收到Unicodeescape错误:

SyntaxError: (unicode error) unicodeescape codec can't decode bytes in position 0-1: malformed \N character escape

I know it's something to do with the way Python interprets 'Users', but haven't had much luck adding r. 我知道这与Python解释“用户”的方式有关,但添加r并没有太多运气。 or adding double slashes (I've probably put them in the wrong place!) 或添加双斜杠(我可能把它们放在错误的位置了!)

Any help gratefully received. 非常感谢任何帮助。

folder_year = str(datetime.today().year) #Save current year as a variable
folder_month = str((datetime.today()- relativedelta(months=1)).strftime("%B")) #Save current month as a variable

yr_directory = "C:\\Users\\Beva\\Documents\\Lettings Index\\"+str(folder_year) #Current year folder link

full_directory = "C:\\Users\\Beva\\Documents\\Lettings Index\\"+str(folder_year)+"\\"+str(folder_month)+"\New Lets Old" #Current month for current year folder link

You can use raw strings ("add r ") like this: 您可以像这样使用原始字符串 (“ add r ”):

>>> r'\New Lets Old'
'\\New Lets Old'

Or escape the backslash yourself: 或者自己逃脱反斜杠:

>>> '\\New Lets Old'
'\\New Lets Old'

You're getting the error because Python tries to interpret the \\N... part as a Unicode code point: 您收到错误消息是因为Python尝试将\\N...部分解释为Unicode代码点:

>>> '\N{LATIN CAPITAL LETTER B}\N{LATIN SMALL LETTER E}\N{LATIN SMALL LETTER V}\N{LATIN CAPITAL LETTER A}'
'BevA'

oops, I couldn't scroll the code in the question to the right originally and didn't see where the \\N came from 糟糕,我原来无法将问题中的代码向右滚动,也看不到\\N来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 115-116 中的字节:格式错误的 \\N 字符转义 - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 115-116: malformed \N character escape UnicodeDecodeError:“unicodeescape”编解码器无法解码 position 37988-37989 中的字节:格式错误的 \N 字符转义 - UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 37988-37989: malformed \N character escape python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误 - python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXXX escape , on an image - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape , on an image Tkinter:SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码位置 2-3 中的字节:截断的 \\UXXXXXXXX 转义 - Tkinter: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码 position 7-8 中的字节:截断 \UXXXXXXXX 转义 - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 7-8: truncated \UXXXXXXXX escape SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误使用 Selenium 和 Python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error using Selenium and Python re.compile("[" ^ SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 0-7 的字节:截断的 \\UXXXXXXXX 转义 - re.compile("[" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-7: truncated \UXXXXXXXX escape “语法错误:(unicode 错误)'unicodeescape' 编解码器无法解码 position 2-3 中的字节:截断 \UXXXXXXXX 转义”。 (文件管理错误) - "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" . (File management bug) Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义 - Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM