Sat.N
Sat.N

Reputation: 85

I am getting extra column while converting multiple excel '.xlsx' to '.csv' files in python?

i am trying to convert multiple excel file '.xlsx' to '.csv' using pandas in python. i am able to convert multiple excel file in csv but i am getting an extra column at the beginning of '.csv' file.

here is my code-

import pandas as pd,xlrd,glob

excel_files = glob.glob(r"C:\Users\Videos\file reader\*.xlsx")
for excel_file in excel_files:

  print("Converting '{}'".format(excel_file))
  try:
      df = pd.read_excel(excel_file)
      output = excel_file.split('.')[0]+'.csv'
      df.to_csv(output)
  except KeyError:
      print("  Failed to convert")

input-

enter image description here

output-

enter image description here

As we can see in output file there is an extra column. can anyone show me how can i remove it ?

Thanks

Upvotes: 4

Views: 1007

Answers (1)

anky
anky

Reputation: 75080

set df.to_csv(output,index=False)

full code:

import pandas as pd,xlrd,glob

excel_files = glob.glob(r"C:\Users\Videos\file reader\*.xlsx")
for excel_file in excel_files:

  print("Converting '{}'".format(excel_file))
  try:
      df = pd.read_excel(excel_file)
      output = excel_file.split('.')[0]+'.csv'
      df.to_csv(output,index=False)
  except KeyError:
      print("  Failed to convert")

Upvotes: 4

Related Questions