0

How can I combine multiple files with different extensions (e.g., xlsx and csv) into one file csv or excel using Python?

For example, suppose I have the following files:

Folder_A/a1.csv Folder_B/b1.xlsx Folder_B/b2.xlsx

I want to combine these in row wise.

Here is my current working code but it is not the smart way to write each file name. enter image description here

Jaya Raghavendra
  • 1,211
  • 1
  • 8
  • 9
  • 1
    Suppose we have a/a1.csv, b/b1.xlsx and b/b2.xlsx, are you looking to concatenate the rows in all 3 files one below the other? Could you edit the description to explain how you want to combine these files please? – S Anand Apr 08 '22 at 06:56
  • please check my edited explanation once:) – Jaya Raghavendra Mar 03 '23 at 22:59

1 Answers1

0
  1. For file handling I recommend using the pathlib built-in python module: pathlib examples. Use the glob method to fetch all files with a given ending - .csv and .xslx
  2. Next you can use pandas to open the csv and .xslx files - check these examples for csv files, for excel files
  3. Once you load the data into dataframes, you can combine them into one dataframe. If necessary do some data manipulation on the columns.
  4. And lastly you can export the combined dataframe into a csv file - use the pd.to_csv() method - documentation on the method