“Unicode Error ”unicodeescape\" codec can't decode bytes… Cannot open text files in Python 3

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

I am using python 3.1, on a windows 7 machines. 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 (, 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 (, 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 (, 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 (, line 1)#44>#41>#40>#39>

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?

回答1:

The problem is with the string

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

Here, \U 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).



回答2:

Typical error on Windows because the default user directory is C:\user\, so when you want to use this path as an string parameter into a Python function, you get a Unicode error, just because the \u is a Unicode escape. Any character not numeric after this produces an error.

To solve it, just double the backslashes: C:\\\user\\\<\your_user>...



回答3:

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')


回答4:

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 \\ [double-backslashes] here - maintains readability and works well.



回答5:

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.



回答6:

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



回答7:

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



回答8:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!