Unable to read .xlsx file using pandas

MRAB python at mrabarnett.plus.com
Thu Dec 3 07:54:54 EST 2020


On 2020-12-03 12:13, A. M. Thomas [PETech MIET MBA] wrote:
> Kindly help manage read .xlsx files using pandas, thank you
> 
> import numpy as np
> import pandas as pd
> from pandas import Series, DataFrame
> 
> excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics
> Internship - DemoS2.xlsx')
> dframe = excelfile.parse('Sheet10')
> print (dframe)
> 
> 
> C:\Users\THOMAS\AppData\Local\Programs\Python\Python39\python.exe
> "C:/Users/THOMAS/PycharmProjects/Assignment#2/code File.py"
>    File "C:\Users\THOMAS\PycharmProjects\Assignment#2\code File.py", line 5
>      excelfile = pd.ExcelFile('C:\Users\THOMAS\Documents/Hash Analytics
> Internship - DemoS2.xlsx')
> 
>                      ^
> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
> position 2-3: truncated \UXXXXXXXX escape
> 
> Process finished with exit code 1
> 
You're giving the path as a plain string that contains \U, which is the 
start of an escape sequence, but it's not a valid escape sequence, hence 
the error.

You could use a raw string:

     r'C:\Users\THOMAS\Documents/Hash Analytics Internship - DemoS2.xlsx'

or escape the backslashes:

     'C:\\Users\\THOMAS\\Documents/Hash Analytics Internship - DemoS2.xlsx'

or use slashes instead of backslashes:

     'C:/Users/THOMAS/Documents/Hash Analytics Internship - DemoS2.xlsx'


More information about the Python-list mailing list