I'm trying to read an csv file into python (spyder) but I keep getting an error. My code looks as follows:
import csv
data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)
and 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. I hope someone can tell me what I have to do.. Thanks in advance
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. I don't know Python specifically but I'd guess you do it by doubling the backslashes:
As per String literals:
So ideally you need to replace the line:
To any one of the following characters:
Using raw prefix and single quotes (i.e.
'...'
):Using double quotes (i.e.
"..."
) and escaping backslash character (i.e.\
):Using double quotes (i.e.
"..."
) and forwardslash character (i.e./
):This error occurs because you are using a normal string as a path. You can use one of the following solutions to fix your problem.
r
before your normal string it converts normal string to raw string:pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")
2:
3:
The double \ should work for Windows, but you still need to take care of the folders you mention in your path. All of them (exept the filename) must exist. otherwise you will get an error.
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"
You can just put
r
in front of the string with your actual path, which denotes a raw string. For example: