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

Miesje picture Miesje · May 23, 2016 · Viewed 839.4k times · Source

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.

Answer

DeePak M. Birajdar picture DeePak M. Birajdar · Sep 2, 2017

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 normal string to 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")