python - 在 python 中将多个 excel '.xlsx' 文件转换为 '.csv' 文件时,我得到了额外的列?

标签 python excel python-3.x pandas csv

我正在尝试在 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")

输入-

enter image description here

输出-

enter image description here

正如我们在输出文件中看到的那样,有一个额外的列。谁能告诉我 我怎样才能删除它?

谢谢

最佳答案

设置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/

相关文章:

python - 使用 Google Compute Engine 时,“EntryPoint”对象没有属性 'resolve'

vba - VBA 中相对引用的条件格式不适用于德语 Excel

python - 多个类别的出现顺序

python - 属性错误 : 'function' object has no attribute 'func_name' and python 3

python - 在 Python 中,如何在一行代码中创建一个包含 n 个字符的字符串?

python - 在 while 循环中更改 tkinter Canvas 中的图像

python - 如何从 Python 中的列表列表中删除所有无元素

vba - 将自定义数据表添加到 VBA 中的图表对象

python - 在Opencv中将图像与深度图对齐

python - 返回类中的元组列表