0

I have installed pandas package in pycharm, trying to read an excel file but it is showing error.

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

Here are my codes I tried:

import pandas as pd
Book1 = pd.read_excel(File_path="C:\Users\shubh\Desktop\Book1.xlsx", sheet_name="Sheet1")
print(Book1)
Itai Klapholtz
  • 196
  • 1
  • 2
  • 15

3 Answers3

1

I think you need to replace \ with \\

Suhas Mucherla
  • 1,383
  • 1
  • 5
  • 17
1

Try adding r: in front of your code, and replace backward slash with forward slash:

Book1 = pd.read_excel(File_path=r"C:/Users/shubh/Desktop/Book1.xlsx", sheet_name="Sheet1")

There are loads of sources to read from about what r: does to your path.

The r'..' string modifier causes the '..' string to be interpreted literally. That means, r'My\Path\Without\Escaping' will evaluate to 'My\Path\Without\Escaping' - without causing the backslash to escape characters. The prior is equivalent to 'My\\Path\\Without\\Escaping' string, but without the raw modifier.

Note: The string cannot end with an odd number of backslashes, i.e r'Bad\String\Example\' is not a correct string.

Taken from: Unknown python expression filename=r'/path/to/file'

sophocles
  • 13,593
  • 3
  • 14
  • 33
0

Just replace "" for "/" in Your path

Franciszek Job
  • 388
  • 2
  • 9