0

I want to read Test.xlsx but the file is in another folder named 1. Is there a way to specify the file location file_loc? I tried this but there seems to be an error.

import pandas as pd
import numpy as np


file_loc = "C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"

df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
A=df["N"].to_numpy()
print([A])

The error is

line 11
    file_loc = "C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"
                                                                                                       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
3
  • one possible reason - your excel data is having some unsupported characters Commented Sep 12, 2022 at 3:28
  • The error is in specifying the file location, not the contents of the file as shown above. Commented Sep 12, 2022 at 3:34
  • 1
    You need to escape the \ characters in the path; file_loc = "C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\1\\Test.xlsx" Commented Sep 12, 2022 at 3:53

1 Answer 1

2

Try to change your path string to a raw string r'C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx'

because \1 converts to an emoji().

Or replace single slash\ with double slash\\ "C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\1\\Test.xlsx"

import pandas as pd
import numpy as np


file_loc = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"

df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
A=df["N"].to_numpy()
print([A])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.