简体   繁体   English

unicode错误)“ unicodeescape”编解码器无法解码位置9-10中的字节

[英]unicode error) 'unicodeescape' codec can't decode bytes in position 9-10

I Have below python code with simple assignements. 我下面有简单分配的python代码。

import datetime
from datetime import date
failed_script_name="Z:\Python\FAILED_CODE"+str(date.today())+".txt"
fail_script_name="Z:\Python\FAIL_CODE"+str(date.today())+".txt"
need_script_name="Z:\Python\NEED_CODE_"+str(date.today())+".txt"

and I am getting error in third assignment. 我在第三次作业中遇到错误。 If i comment the third assignment its working. 如果我评论第三个任务,它的工作。 I don't get whats wrong with third assignment as it is same as the first two 我没有错第三次作业,因为它与前两次相同

File "Z:\Python\test.py", line 5
    need_script_name="Z:\Python\NEED_CODE_"+str(date.today())+".txt"
                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
position 9-10: malformed \N character escape

The backslash \\ is used as an escape character in Python string literals. 反斜杠\\用作Python字符串文字中的转义字符。 An escape character is used to represent characters, which can not be typed directly into the source code. 转义字符用于表示字符,不能直接在源代码中键入。

If the \\ is encountered in a string literal, python will determine what to do by looking at the next character. 如果在字符串文字中遇到\\ ,Python将通过查看下一个字符来确定要执行的操作。 For example N indicates to Python, that the following brace-enclosed sequence of characters should be read as the name of a Unicode character, which will then inserted at that point, eg 例如, N表示Python,应将以下用大括号括起来的字符序列读取为Unicode字符的名称,然后将在该点插入该字符,例如

>>> print("\N{RIGHTWARDS ARROW}")
→

However in your case you follow \\N by something else which cannot be interpreted that way. 但是,在您的情况下,您会在\\N后面加上其他无法解释的内容。 Therefore the error message. 因此出现错误信息。

In the case of P and F after \\ , these are assigned no special meaning in Python and by design Python will in such cases leave both the \\ and the following character unchanged in the string literal. 对于\\\\之后的PF ,在Python中没有给它们指定特殊含义,并且在设计上,Python将在这种情况下将\\和后面的字符保留在字符串文字中。 Therefore these don't generate an error or any problem for you: 因此,这些不会为您产生错误或任何问题:

>>> print("\P\F")
\P\F

One can properly insert a \\ in a string literal by following it with another \\ , which indicates to Python that you want to have a single backslash in the string literal: 一个人可以在一个字符串文字中正确地插入一个\\ ,之后跟另一个\\ ,这向Python表示您希望在字符串文字中使用一个反斜杠:

>>> print("\\N{RIGHTWARDS ARROW}")
\N{RIGHTWARDS ARROW}

Alternatively you can turn off the special meaning of \\ , by prefixing the string literal with a r , making it a raw string literal : 另外,您可以通过在字符串文字前加上r使其成为原始字符串文字来关闭\\的特殊含义:

>>> print(r"\N{RIGHTWARDS ARROW}")
\N{RIGHTWARDS ARROW}

Also see the Python documentation which has a list of escape sequences and their interpretation. 另请参阅Python文档,其中提供了转义序列及其解释的列表。

The answer above assumes Python 3.x. 上面的答案假设使用Python3.x。 In Python 2.x \\N would not be assigned any special meaning in a usual string, ie "\\Nonesense" would only generate an error if it was prefixed by u to make it a unicode string literal u"\\Nonesense" , for which the escape sequence also applies in Python 2. 在Python 2.x中, \\N在通常的字符串中不会被赋予任何特殊的含义,即"\\Nonesense"仅在以u前缀使其成为unicode字符串文字u"\\Nonesense"才会产生错误。转义序列也适用于Python 2。

See also Python 2.x documentation. 另请参阅Python 2.x文档。

暂无
暂无

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

相关问题 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 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 (unicode错误)'unicodeescape'编解码器无法解码字节 - 字符串带'\\ u' - (unicode error) 'unicodeescape' codec can't decode bytes - string with '\u' 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错误)“ unicodeescape”编解码器无法解码位置0-1的字节:格式错误的\\ N字符转义 - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape (unicode错误)'unicodeescape'编解码器无法解码位置16-17中的字节:截断\\ uXXXX转义 - (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \uXXXX escape 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 using Selenium Python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python 转换为Jython Python 3.5项目 - UnicodeDecodeError:'unicodeescape'编解码器无法解码位置4-10中的字节:非法Unicode字符 - Converting to Jython a Python 3.5 project - UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 4-10: illegal Unicode character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM