Pandas read files

Posted on December 20th, 2020
Author: Matteo Buldrini

Sometimes when reading a file in Pandas you may incur in the following error after declaring the path name:

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

This happens because path names tend to have backslashes in them (e.g. 'C:\Documents\File.csv'). In Python, backslash is used to signify special characters, but when we use it in a path name we want to refer to actual backslashes, not to special characters.

To solve this issue, you need to add an r before the path name, so that Python can interpret backslashes as strings.

Example:

df = pd.read_csv(r'C:\Documents\File.csv')