1

I've been trying to open an excel file in python, but so far it has not worked. My code is the following:

import pandas as pd
from openpyxl.workbook import Workbook
df_excel = pd.read_excel('‪C:\Users\Adam Smith\Desktop\GPA Scale.xlsx')
print (df_excel)

The error I get is the following:

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

I've tried the following but the error is still not fixed.

  1. I tried to put double slashes as follows: df_excel = pd.read_excel('‪C:\\Users\\Adam Smith\\Desktop\\GPA Scale.xlsx') I got the following error when I put the double slashes OSError: [Errno 22] Invalid argument: '\u202aC:\\Users\\Adam Smith\\Desktop\\GPA Scale.xlsx

  2. I tried to add an 'r' at the beginning of the line as follows:

    df_excel = pd.read_excel(r'‪C:\Users\Adam Smith\Desktop\GPA Scale.xlsx') I got the following error when I added the 'r' OSError: [Errno 22] Invalid argument: '\u202aC:\\Users\\Adam Smith\\Desktop\\GPA Scale.xlsx'

  3. Lastly, I tried to change the backslash to forward slash as follows:

    df_excel = pd.read_excel('‪C:/Users/Adam Smith/Desktop/GPA Scale.xlsx')

I got the following error when I changed it to a forward slash.

`OSError: [Errno 22] Invalid argument: '\u202aC:/Users/Adam Smith/Desktop/GPA Scale.xlsx'`

I'm confused as to why I keep getting the error. Any suggestions on fixing it would be appreciated.

Thanks

  • 1
    Possible duplicate https://stackoverflow.com/questions/49267999/remove-u202a-from-python-string Did you copy-paste the path string from somewhere? If so, try to type it again. There is a leading `\u202a` (which is RTL related) before the actual path – DeepSpace Aug 06 '20 at 22:57
  • @DeepSpace, you are right! Thanks! It worked! I had to type it in and the \u202a was gone! Much appreciated. – Ved Brahmbhatt Aug 06 '20 at 23:17
  • David Cullen, thanks for the link, it provides good a good theoretical understanding of why it happens and DeepSpace's answer provides a practical solution. Perfect combo. – Ved Brahmbhatt Aug 06 '20 at 23:18
  • Answer can be found here https://stackoverflow.com/questions/37400974/error-unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3 – Kyoujin Oct 20 '22 at 10:33

1 Answers1

-1

Try this:

from pathlib import Path
import pandas as pd

filename = r'C:\Users\Adam Smith\Desktop\GPA Scale.xlsx'  # r'...' => raw string
filename = Path(filename)

with open(filename, 'rb') as handle:  # rb => read binary
    df = pd.read_excel(handle)

jsmart
  • 2,921
  • 1
  • 6
  • 13