By Eric


2009-08-28 15:36:39 8 Comments

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 (<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?

10 comments

@Martin v. Löwis 2009-08-28 15:46:24

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"

@Humphrey Bogart 2010-02-24 00:04:51

Hahaha... What an easy trap to fall into, like I just have. Awesome answer.

@Srid says Reinstate Monica 2010-04-28 20:16:16

I found this error in a function docstring while porting a 2.x code to python3.

@Chris Mueller 2015-05-22 15:46:37

I ran into this error when I used triple quote ''' comments around a section of code that contained a raw string with a \U in it. I.E. the string didn't give me an error until I tried to comment it out. For this reason the double backslash method might be preferred.

@Bobort 2016-10-17 21:01:42

I am prefixing the string with r, but it is still giving me the error.

@Reman 2017-11-30 08:44:40

I have the same error but my string is a variable. No possibility to put an 'r' before the variable. How do I resolve this?

@Jona 2018-04-17 09:17:09

A third solution (also proposed by @Adam Baylin Autuori) is to replace '\' with '/' in the path. Python understand both '\' (Windows-style) or '/' (Unix-style) as directory separator.

@Poete Maudit 2018-06-20 21:27:40

This answer does not work for me either. If I am right, the fact that the backslashes are doubled makes the directory wrong so I get a FileNotFoundError.

@basavaraj_gowda 2019-12-12 05:45:26

Thanks for the help!.

@Julio Cesar 2015-03-12 16:04:47

Typical error on Windows because the default user directory is C:\user\<your_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>...

@RaelB 2019-06-03 08:27:21

You say "Just double the backslashes", but your example is with 3 backslashes?

@Julio Cesar 2019-06-04 16:02:43

Fixed. Thanks @RaelB

@Fiddy Bux 2017-08-16 21:30:23

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.

@PCSailor 2018-09-22 02:00:28

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.

@Ravi Teja Mureboina 2018-05-25 13:16:22

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.

@Matthew Cox 2017-12-21 20:30:28

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

@tripleee 2019-03-30 13:22:01

This doesn't add anything over the accepted answer.

@Andrushenko Alexander 2017-05-04 20:33:32

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

@Fiddy Bux 2018-12-07 20:20:26

This may have worked for you, and it may well help others, but it would be good if you could explain why this worked, so as to provide others with a potentially viable solution if the reinstall method fails. Don't worry, if not. At this stage in my learning I don't have a solid idea why a numpy reinstall worked in this case either.

@Deepika Anand 2015-02-08 10:26:32

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

@SuperBiasedMan 2015-11-03 09:35:25

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.

@Java.beginner 2015-11-26 11:47:35

Thanks @Deepika Anand, its works, could please explain how? thx

@Deepika Anand 2015-11-28 05:26:44

Refer the official documentation on page :docs.python.org/2/howto/unicode.html . specially the line "In Python source code, Unicode literals are written as strings prefixed with the ā€˜uā€™ or ā€˜Uā€™ character: u'abcdefghijk'. Specific code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point. The \U escape sequence is similar, but expects 8 hex digits, not 4."

@Adam Baylin Autuori 2015-11-03 08:38:58

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

@rene 2015-11-03 08:46:07

I advice you to take the tour and visit the help center. Your answer doesn't seem to meet our quality standards. You need to elaborate a bit why this would work, maybe create the complete code example.

@SuperBiasedMan 2015-11-03 09:38:22

Welcome to Stack Overflow! Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.

@user2859901 2013-10-08 18:38:37

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.

@Johnny 2017-11-18 03:12:24

This would be a workaround but the correct answer is listed above.

Related Questions

Sponsored Content

10 Answered Questions

8 Answered Questions

[SOLVED] Writing Unicode text to a text file?

2 Answered Questions

[SOLVED] Address error: (unicode error) 'unicodeescape' codec can't decode

  • 2017-10-30 15:21:18
  • Praemium
  • 782 View
  • 2 Score
  • 2 Answer
  • Tags:   python image

2 Answered Questions

[SOLVED] Error reading csv file unicodeescape

2 Answered Questions

[SOLVED] unicode error preventing creation of text file

  • 2013-11-06 03:38:52
  • user2828832
  • 652 View
  • 1 Score
  • 2 Answer
  • Tags:   unicode python-3.x

Sponsored Content