Home > Blockchain >  Reading a File in Pandas with quote symbol in Filename
Reading a File in Pandas with quote symbol in Filename

Time:02-01

I have a file name which has say the following (with apostrophe in file name)

Path = 'C:\xxx\yyy-testing\my_inner_path'

FileName = 'ABC'01myFile.xlsx'

so define File = Path FileName

I am trying to read the file using the pandas route

  mydf = pd.read_excel(File, engine='openpyxl', sheet_name=sheet_name)

However, it is giving me error as it cannot read the apostrophe it seems. Any workaround please ( without removing the apostrophe) ?

CodePudding user response:

First you have to use double quotes instead of single ones or use \ to escape the character. Second, don't use \ when you use path in Python. If you have a folder like C:\Users, python will complain about unicode error. Third, you should use pathlib to avoid add strings.

import pathlib

Path = pathlib.Path('C:/xxx/yyy-testing/my_inner_path')
FileName = "ABC'01myFile.xlsx"

File = Path / FileName

mydf = pd.read_excel(File, engine='openpyxl', sheet_name=sheet_name)

CodePudding user response:

Just make your FileName inside ("") to escape the apostrophe :

Path = r'C:\xxx\yyy-testing\my_inner_path'

FileName = "\ABC'01myFile.xlsx"

File = Path   FileName

mydf = pd.read_excel(File, engine='openpyxl', sheet_name=sheet_name)

Or use, the backslash (\) :

FileName = '\ABC\'01myFile.xlsx'
  • Related