[英]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.