Can I append csv end xlsx files to the same list in Python?

Viewed 41

Imagine you have the same structure of table in a xlsx file and a csv. Can I append it together?

Something like this:

filesList = ['file.xlsx', 'file.csv']
files = []

for file in filesList:
    if file.suffix = 'xlsx':
        a = pd.read_excel(file)
    if file.suffix = 'csv'
        a = pd.read_csv(file)

files.append(a)
1 Answers

There is no suffix attribute for Python strings. Pandas does not have a read_xlsx function.

You probably want something like this:

filesList = ['file.xlsx','file.csv']
files = [] # dataframes

for file in filesList:
  if file.endswith('.xlsx'):
    files.append(pd.read_excel(file))
  elif file.endswith('.csv'):
    files.append(pd.read_csv(file))
Related