Reputation: 85
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-
output-
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
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