Pandas read files
Sometimes when reading a file in Pandas you may incur in the following error after declaring the path name:
1 2 3 4 5 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.
1 2 3 df = pd.read_csv(r'C:\Documents\File.csv')