我正在尝试在 python 中使用 pandas 将多个 excel 文件“.xlsx”转换为“.csv”。我能够在 csv 中转换多个 excel 文件,但我在“.csv”文件的开头得到了一个额外的列。
这是我的代码-
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")
输入-
输出-
正如我们在输出文件中看到的那样,有一个额外的列。谁能告诉我 我怎样才能删除它?
谢谢
最佳答案
设置df.to_csv(output,index=False)
完整代码:
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")
关于python - 在 python 中将多个 excel '.xlsx' 文件转换为 '.csv' 文件时,我得到了额外的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371834/