0

The only thing I added was the attempted concatenation of the file path.

I am getting an 'unexpected character after line continuation character, and cannot figure out why.

import numpy as np
import pandas as pd
import getpass

user = getpass.getuser()

data = pd.read_excel (r'C:\Users\' + user + '\Desktop\bulk export.xlsx',
                       sheet_name=1,
                       header=0)
df = pd.DataFrame(data, 
                  columns= [1,'Record Type'])
print (df)
2
  • escape character: r'C:\Users\\', same for the other part as well. Commented Oct 29, 2021 at 14:32
  • Try f-strings too: pd.read_excel(rf'C:\Users\{user}\Desktop\bulk export.xlsx', sheet_name=1, header=0) Commented Oct 29, 2021 at 14:38

1 Answer 1

1

You can try this:

import pathlib
from pathlib import Path

user = getpass.getuser()
my_file = Path(f"C:\\Users\\{user}\\Desktop\\bulk export.xlsx")

data = pd.read_excel (my_file, sheet_name=1, header=0) 
Sign up to request clarification or add additional context in comments.

2 Comments

why does this work without the 'r' in the front of the path name?
@anarchocaps, r in front of the path makes it a raw string meaning that the \ is treated as literal. In this case \ is also an escape character meaning "\\" is literal "\". You can see this by trying print("\\").

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.