&quot'unicode错误' unicodeescape'编解码器可以解释字节...编写Windows文件路径时

发布于 2025-01-21 22:39:27 字数 1511 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

jJeQQOZ5 2025-01-28 22:39:27

问题在于此处的字符串

"C:\Users\Eric\Desktop\beeline.txt"

\ u in “ c:\ users ...启动八个字符的Unicode Escape,例如\ code> \ u00014321

>

"C:\\Users\\Eric\\Desktop\\beeline.txt"

。 :

r"C:\Users\Eric\Desktop\beeline.txt"

The problem is with the string

"C:\Users\Eric\Desktop\beeline.txt"

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:

"C:\\Users\\Eric\\Desktop\\beeline.txt"

Or prefix the string with r (to produce a raw string):

r"C:\Users\Eric\Desktop\beeline.txt"
那支青花 2025-01-28 22:39:27

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.

哀由 2025-01-28 22:39:27

'r'的前缀效果很好,但是它需要使用正确的语法。例如:

passwordFile = open(r'''C:\Users\Bob\SecretPasswordFile.txt''')

这里不需要\\ - 保持可读性并效果很好。

Prefixing with 'r' works very well, but it needs to be in the correct syntax. For example:

passwordFile = open(r'''C:\Users\Bob\SecretPasswordFile.txt''')

No need for \\ here - maintains readability and works well.

孤独患者 2025-01-28 22:39:27

有了Python 3,我遇到了这个问题:

 self.path = 'T:\PythonScripts\Projects\Utilities'

产生了此错误:

 self.path = 'T:\PythonScripts\Projects\Utilities'
            ^
 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
 position 25-26: truncated \UXXXXXXXX escape

有效的修复程序是:

 self.path = r'T:\PythonScripts\Projects\Utilities'

“ \ u”似乎正在产生错误,并且在字符串之前关闭了八个字符的Unicode Unicode Escape(用于原始字符串) )失败了。 (这有点过分简化,但是如果您不关心Unicode,它可以有效)

希望这对某人有帮助

With Python 3 I had this problem:

 self.path = 'T:\PythonScripts\Projects\Utilities'

produced this error:

 self.path = 'T:\PythonScripts\Projects\Utilities'
            ^
 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
 position 25-26: truncated \UXXXXXXXX escape

the fix that worked is:

 self.path = r'T:\PythonScripts\Projects\Utilities'

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

櫻之舞 2025-01-28 22:39:27
path = pd.read_csv(**'C:\Users\mravi\Desktop\filename'**)

错误是由于提到的路径

添加'r'在路径之前

path = pd.read_csv(**r'C:\Users\mravi\Desktop\filename'**)

可以正常工作。

path = pd.read_csv(**'C:\Users\mravi\Desktop\filename'**)

The error is because of the path that is mentioned

Add 'r' before the path

path = pd.read_csv(**r'C:\Users\mravi\Desktop\filename'**)

This would work fine.

旧时模样 2025-01-28 22:39:27

或者,您可以在路径中用'/'替换“ \”。

Or you could replace '\' with '/' in the path.

赢得她心 2025-01-28 22:39:27

我在Python 3.2中遇到了同样的错误。

我有用于电子邮件发送的脚本,并且

csv.reader(open('work_dir\uslugi1.csv', newline='', encoding='utf-8'))

当我删除文件中的第一个char uslugi1.csv工作正常。

I had this same error in python 3.2.

I have script for email sending and:

csv.reader(open('work_dir\uslugi1.csv', newline='', encoding='utf-8'))

when I remove first char in file uslugi1.csv works fine.

挽你眉间 2025-01-28 22:39:27

请参阅OpenPyXl文档,您可以按照以下方式进行更改。

from openpyxl import Workbook
from openpyxl.drawing.image import Image

wb = Workbook()
ws = wb.active
ws['A1'] = 'Insert a xxx.PNG'
# Reload an image
img = Image(**r**'x:\xxx\xxx\xxx.png')
# Insert to worksheet and anchor next to cells
ws.add_image(img, 'A2')
wb.save(**r**'x:\xxx\xxx.xlsx')

Refer to openpyxl document, you can do changes as followings.

from openpyxl import Workbook
from openpyxl.drawing.image import Image

wb = Workbook()
ws = wb.active
ws['A1'] = 'Insert a xxx.PNG'
# Reload an image
img = Image(**r**'x:\xxx\xxx\xxx.png')
# Insert to worksheet and anchor next to cells
ws.add_image(img, 'A2')
wb.save(**r**'x:\xxx\xxx.xlsx')
谷夏 2025-01-28 22:39:27

我遇到了同样的错误,只是卸载并再次安装了Numpy软件包!

I had same error, just uninstalled and installed again the numpy package, that worked!

九局 2025-01-28 22:39:27

我有这个错误。
我有一个主要的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文