&quot'unicode错误' unicodeescape'编解码器可以解释字节...编写Windows文件路径时
我在Windows 7机器上使用Python 3.1。俄语是默认的系统语言,UTF-8是默认编码。
查看a 上一个问题的答案 “编解码器”模块给我一点运气。这里有几个例子:
>>> g = codecs.open("C:\Users\Eric\Desktop\beeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:\Python31\Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed \N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#44>, line 1)
我的最后一个想法是,我认为Windows“翻译”了一些文件夹,例如“用户”文件夹,将其转换为俄语(尽管输入“用户”仍然是正确的路径),因此,我在Python31文件夹中尝试了它。仍然没有运气。有什么想法吗?
I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding.
Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here's a few examples:
>>> g = codecs.open("C:\Users\Eric\Desktop\beeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:\Python31\Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed \N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#44>, line 1)
My last idea was, I thought it might have been the fact that Windows "translates" a few folders, such as the "users" folder, into Russian (though typing "users" is still the correct path), so I tried it in the Python31 folder. Still, no luck. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
问题在于此处的字符串
,
\ u
in“ c:\ users
...启动八个字符的Unicode Escape,例如\ code> \ u00014321
>
。 :
The problem is with the string
Here,
\U
in"C:\Users
... starts an eight-character Unicode escape, such as\U00014321
. In your code, the escape is followed by the character 's', which is invalid.You either need to duplicate all backslashes:
Or prefix the string with
r
(to produce a raw string):Windows上的典型错误是因为默认用户目录是
c:\ user \&lt; your_user&gt;
,因此,当您想将此路径作为字符串参数传递到python函数时,您会得到一个Unicode错误,仅仅因为\ u
是Unicode逃生。如果\ u
不是数字之后的接下来8个字符,则会产生错误。要解决它,只需加倍反斜击:
c:\\ user \\&lt; \ your_user&gt; ...
这将确保Python将单个后斜线视为单个后斜线。
Typical error on Windows because the default user directory is
C:\user\<your_user>
, so when you want to pass this path as a string argument into a Python function, you get a Unicode error, just because the\u
is a Unicode escape. If the next 8 characters after the\u
are not numeric this produces an error.To solve it, just double the backslashes:
C:\\user\\<\your_user>...
This will ensure that Python treats the single backslashes as single backslashes.
以
'r'
的前缀效果很好,但是它需要使用正确的语法。例如:这里不需要
\\
- 保持可读性并效果很好。Prefixing with
'r'
works very well, but it needs to be in the correct syntax. For example:No need for
\\
here - maintains readability and works well.有了Python 3,我遇到了这个问题:
产生了此错误:
有效的修复程序是:
“ \ u”似乎正在产生错误,并且在字符串之前关闭了八个字符的Unicode Unicode Escape(用于原始字符串) )失败了。 (这有点过分简化,但是如果您不关心Unicode,它可以有效)
希望这对某人有帮助
With Python 3 I had this problem:
produced this error:
the fix that worked is:
It seems the '\U' was producing an error and the 'r' preceding the string turns off the eight-character Unicode escape (for a raw string) which was failing. (This is a bit of an over-simplification, but it works if you don't care about unicode)
Hope this helps someone
错误是由于提到的路径
添加
'r'
在路径之前可以正常工作。
The error is because of the path that is mentioned
Add
'r'
before the pathThis would work fine.
或者,您可以在路径中用'/'替换“ \”。
Or you could replace '\' with '/' in the path.
我在Python 3.2中遇到了同样的错误。
我有用于电子邮件发送的脚本,并且
当我删除文件中的第一个char
uslugi1.csv
工作正常。I had this same error in python 3.2.
I have script for email sending and:
when I remove first char in file
uslugi1.csv
works fine.请参阅OpenPyXl文档,您可以按照以下方式进行更改。
Refer to openpyxl document, you can do changes as followings.
我遇到了同样的错误,只是卸载并再次安装了Numpy软件包!
I had same error, just uninstalled and installed again the numpy package, that worked!
我有这个错误。
我有一个主要的python脚本,该脚本从另一个,第二个,python脚本中调用函数。
在第一个脚本的结尾,我有一个注释块,该块指定了
'''''''
。由于这个评论代码块,我遇到了这个错误。
一旦找到错误,我多次重复了错误,以确保这是错误,&amp;是。
我仍然不确定为什么。
I had this error.
I have a main python script which calls in functions from another, 2nd, python script.
At the end of the first script I had a comment block designated with
''' '''
.I was getting this error because of this commenting code block.
I repeated the error multiple times once I found it to ensure this was the error, & it was.
I am still unsure why.