Home > Enterprise >  How to use variable name within dataframe path
How to use variable name within dataframe path

Time:10-30

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)

CodePudding user response:

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) 
  • Related