494

I'm trying to read a CSV file into Python (Spyder), but I keep getting an error. My code:

import csv

data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)

I get the following error:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I have tried to replace the \ with \\ or with / and I've tried to put an r before "C.., but all these things didn't work.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Miesje
  • 4,937
  • 3
  • 10
  • 7
  • 12
    How did your alternative solutions not work? I'd expect either a raw string, or a string with `/`'s instead of `\\`'s to work just fine. – Blckknght May 24 '16 at 03:50
  • when I use double backslashes the program says that the file I want to open doesn't exists. – Miesje May 24 '16 at 10:19
  • 1
    Use '\' forward slash instead of backward slash while specify the path C:/Users/user/Videos changed to C:\Users\user\Videos – Praveen Kumar C Dec 01 '20 at 20:45
  • This is Python bug in the case where it also happens inside multi-line comments (true for v3.7 at least) since Python has no need to scrutinize any text in a comment to look for any encoding to act upon, no? – gseattle Jan 10 '23 at 23:24

10 Answers10

836

This error occurs, because you are using a normal string as a path. You can use one of the three following solutions to fix your problem:

1: Just put r before your normal string. It converts a normal string to a raw string:

pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")

2:

pandas.read_csv("C:/Users/DeePak/Desktop/myac.csv")

3:

pandas.read_csv("C:\\Users\\DeePak\\Desktop\\myac.csv")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Techie
  • 8,594
  • 1
  • 13
  • 7
  • 28
    I like the 2nd option, it makes path portable across Windows and Linux. Thanks for Python's shielding the peculiarity of Windows. – Yu Shen Apr 24 '18 at 16:15
  • 4
    Thanks man. The first answer solved my problem.All I did was add the r to make my string raw. – Samuel Nde Aug 26 '18 at 07:23
  • In my case only one \ before the first \ worked: C:\\Users\DeePak\Desktop... – Feri Aug 26 '18 at 11:54
  • this seems to be an issue when the file path is C, using other letters won't give issues when using the windows style "\" – xgg Nov 20 '18 at 15:29
  • 2
    Even with All these Options, it may not work. Please check your folder and file permissions as well whether it is readonly. I had the same issue. I changed the same and it worked – BigData-Guru Aug 15 '20 at 18:34
  • For those using `pathlib`: have a look at [PurePath.as_posix()](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.as_posix), which returns option 2. – djvg May 11 '22 at 12:48
111

The first backslash in your string is being interpreted as a special character. In fact, because it's followed by a "U", it's being interpreted as the start of a Unicode code point.

To fix this, you need to escape the backslashes in the string. The direct way to do this is by doubling the backslashes:

data = open("C:\\Users\\miche\\Documents\\school\\jaar2\\MIK\\2.6\\vektis_agb_zorgverlener")

If you don't want to escape backslashes in a string, and you don't have any need for escape codes or quotation marks in the string, you can instead use a "raw" string, using "r" just before it, like so:

data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 2
    when I use double backslashes the program says that the file I want to open doesn't exists. – Miesje May 24 '16 at 09:27
  • 3
    That sounds promising as it means it now considers the string to be valid – thomasrutter May 24 '16 at 11:49
  • 2
    Right. So next problem is, that file path doesn't exist. Have you omitted a file extension, eg `vektis_agb_zorgverlener.txt`? Windows Explorer will hide file extensions from you by default because it's stupid; you can [fix it](http://windows.microsoft.com/en-gb/windows/show-hide-file-name-extensions) though. – bobince May 25 '16 at 07:02
50

You can just put r in front of the string with your actual path, which denotes a raw string. For example:

data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
Dartmouth
  • 1,069
  • 2
  • 15
  • 22
Mohit Solanki
  • 2,122
  • 12
  • 20
  • 2
    For sake of completeness this removes the ability to escape characters, including a quote mark, within the string so it simply can't be used for strings containing a quote mark, but perfectly appropriate here. – thomasrutter Feb 11 '20 at 22:47
40

Consider it as a raw string. Just as a simple answer, add r before your Windows path.

 import csv

 data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
 data = csv.reader(data)
 print(data)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramineni Ravi Teja
  • 3,568
  • 26
  • 37
24

Try writing the file path as "C:\\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener" i.e with double backslash after the drive as opposed to "C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"

Ibrahim Isa
  • 529
  • 3
  • 4
  • 3
    it works ! could you please tell why the double slash after the drive works? – ayat ullah sony Sep 27 '19 at 09:42
  • I think it's because \U in \Users is seen by the parser as "here comes some Unicode" and the parser is sad when none is provided. You would need to \\U anywhere, as in c:\notusers\\Users as well. – lonstar Apr 24 '23 at 09:40
15

Add r before your string. It converts a normal string to a raw string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Farshad Javid
  • 528
  • 5
  • 10
  • Using Excel from Python on Windows `xl.Workbooks.Open(Filename=r"C:\Users\david\Desktop\xl_HW.xlsm",ReadOnly=1)` ... worked for me. Simplest answer. – user10186832 Mar 15 '23 at 09:09
13

As per String literals:

String literals can be enclosed within single quotes (i.e. '...') or double quotes (i.e. "..."). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).

The backslash character (i.e. \) is used to escape characters which otherwise will have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter r or R. Such strings are called raw strings and use different rules for backslash escape sequences.

In triple-quoted strings, unescaped newlines and quotes are allowed, except that the three unescaped quotes in a row terminate the string.

Unless an r or R prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.

So ideally you need to replace the line:

data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")

To any one of the following characters:

  • Using raw prefix and single quotes (i.e. '...'):

      data = open(r'C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener')
    
  • Using double quotes (i.e. "...") and escaping backslash character (i.e. \):

      data = open("C:\\Users\\miche\\Documents\\school\\jaar2\\MIK\\2.6\\vektis_agb_zorgverlener")
    
  • Using double quotes (i.e. "...") and forwardslash character (i.e. /):

      data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")
    
Community
  • 1
  • 1
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    I don't understand why you are distinguishing between single and double quotes here? Python processes them identically. – snakecharmerb Apr 28 '19 at 14:26
8

Just putting an r in front works well.

eg:

  white = pd.read_csv(r"C:\Users\hydro\a.csv")
Subhashi
  • 4,145
  • 1
  • 23
  • 22
4

It worked for me by neutralizing the '' by f = open('F:\\file.csv')

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vinod
  • 41
  • 1
1

The double \ should work for Windows, but you still need to take care of the folders you mention in your path. All of them (except the filename) must exist. Otherwise you will get an error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
G4W
  • 53
  • 9