[英]python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error
i have written a python code in which i have already assigned variable 'emailentry' a string value.我已经编写了一个 python 代码,其中我已经为变量“emailentry”分配了一个字符串值。 what i want to do now is add that string value in the last line of my text file known as 'wattpad.txt'
我现在要做的是在我的文本文件的最后一行添加该字符串值,称为“wattpad.txt”
i have written this code我写了这段代码
with open("C:\Users\BRS\Desktop\wattpad.txt", 'a') as outfile:
outfile.write(emailentry /n)
and getting error并得到错误
File "C:\Users\BRS\Desktop\wattpad acc maker.py", line 41
with open("C:\Users\BRS\Desktop\wattpad.txt", 'a') as outfile:
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
pls help [Finished in 0.2s]请帮助 [在 0.2 秒内完成]
\U
is the start of an escape sequence in string litterals, as are a few others like \u
, \n
... \U
是字符串字面量中转义序列的开始, 其他一些类似\u
, \n
...
If you want to have litteral backslashes in your string, you can either:如果你想在你的字符串中有文字反斜杠,你可以:
Escape them:逃离他们:
"C:\\Users\\BRS\\Desktop\\wattpad.txt"
or better, use a raw string by prefixing the string with r
:或者更好的是,通过在字符串前面加上
r
来使用原始字符串:
r"C:\Users\BRS\Desktop\wattpad.txt"
or use forward slashes, even on Windows:或使用正斜杠,即使在 Windows 上:
"C:/Users/BRS/Desktop/wattpad.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.