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 is with Ukraine's user avatar

asked May 23, 2016 at 21:36

Miesje's user avatar

4

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's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered Sep 2, 2017 at 6:27

Techie's user avatar

6 Comments

I like the 2nd option, it makes path portable across Windows and Linux. Thanks for Python's shielding the peculiarity of Windows.

2018-04-24T16:15:15.417Z+00:00

Thanks man. The first answer solved my problem.All I did was add the r to make my string raw.

2018-08-26T07:23:37.697Z+00:00

In my case only one \ before the first \ worked: C:\\Users\DeePak\Desktop...

2018-08-26T11:54:55.77Z+00:00

this seems to be an issue when the file path is C, using other letters won't give issues when using the windows style "\"

2018-11-20T15:29:37.353Z+00:00

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

2020-08-15T18:34:23.947Z+00:00

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's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered May 24, 2016 at 1:15

thomasrutter's user avatar

3 Comments

when I use double backslashes the program says that the file I want to open doesn't exists.

2016-05-24T09:27:39.043Z+00:00

That sounds promising as it means it now considers the string to be valid

2016-05-24T11:49:58.277Z+00:00

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 though.

2016-05-25T07:02:42.967Z+00:00

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's user avatar

Dartmouth

1,0892 gold badges17 silver badges23 bronze badges

answered Feb 17, 2017 at 18:35

Mohit Solanki's user avatar

1 Comment

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.

2020-02-11T22:47:41.707Z+00:00

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's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered Feb 27, 2019 at 5:06

Ramineni Ravi Teja's user avatar

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"

answered May 18, 2018 at 22:29

Ibrahim Isa's user avatar

2 Comments

it works ! could you please tell why the double slash after the drive works?

2019-09-27T09:42:20.337Z+00:00

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.

2023-04-24T09:40:15.683Z+00:00

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

Peter Mortensen's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered Nov 12, 2019 at 9:31

Farshad Javid's user avatar

1 Comment

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.

2023-03-15T09:09:25.877Z+00:00

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's user avatar

answered Oct 22, 2018 at 18:17

undetected Selenium's user avatar

1 Comment

I don't understand why you are distinguishing between single and double quotes here? Python processes them identically.

2019-04-28T14:26:48.013Z+00:00

Just putting an r in front works well.

eg:

white = pd.read_csv(r"C:\Users\hydro\a.csv")

answered Dec 12, 2017 at 14:26

Subhashi's user avatar

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

Peter Mortensen's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered Mar 20, 2018 at 10:14

vinod's user avatar

1 Comment

Neutralizing by using single quotes?

2022-09-16T22:28:28.93Z+00:00

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's user avatar

Peter Mortensen

31.2k22 gold badges111 silver badges134 bronze badges

answered Aug 15, 2017 at 13:45

G4W's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.