1

I am trying to read an Excel file to a pandas dataframe. The code is as bellow:

import pandas as pd
import xlrd
df = pd.read_excel('C:\Users\user\Desktop\ConsumersData_English.xlsx')

Unfortunately I'm getting the following error:

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

Using previous answers of similar questions, I tried to put 'r', forward slash, double backslash - but nothing worked. Any ideas?

Ariel
  • 23
  • 4
  • The problem is that the `\U` is seen as an escape sequence. You should use a raw string, or use an operating system that works with forward slashes. – Willem Van Onsem Jan 05 '20 at 09:32
  • Does this answer your question? ["Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3](https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) – E. Zeytinci Jan 05 '20 at 10:46

1 Answers1

0

The problem is that the \U is seen as an escape sequence. You should use a raw string, escape the backslashes manually, or use an operating system that works with forward slashes.

For example:

import pandas as pd
import xlrd
df = pd.read_excel(r'C:\Users\user\Desktop\ConsumersData_English.xlsx')
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I work in jupyter notebook and new to python, can you elaborate more on how to do it manually. @Willem Van Onsem. Thanks! – Ariel Jan 05 '20 at 10:50
  • @Ariel, surely too late to help you, but for others who may be similarly confused, notice that there is an "r" just before the string containing the file spec. That r tells python to treat the string as a raw string, that is, not to interpret the backslash as a special character. – mermaldad Mar 30 '23 at 13:10