缺少 usecols 时如何迭代另一个 xlsx

How to iterate over another xlsx when usecols is missing

这里是需要您帮助的地方。

我有多个 xlsx 文件,我在每个文件中查找相同的列信息。到目前为止一切正常,但一些 *.xlsx 文件不包含数据,我的 python 脚本只是停止但查看其他文件。

import glob
import pandas as pd

# Setup variables
xlsx_input = 'D:\script\bdd\xlsx\*.xlsx'
csv_output = 'D:\script\bdd\csv\'

# Save all file matches: xlsx_files
xlsx_files = glob.glob(xlsx_input, recursive=True)
# Create an empty list: frames
frames = []
#  Iterate over xlsx_files
for file in xlsx_files:
    #  Read xlsx into a DataFrame
    df = pd.read_excel(file , usecols=['ref_01','ref_02','ref_03'])
    # Append df to frames
    frames.append(df)

# Concatenate frames into dataframe
excel_output = pd.concat(frames)
# Write CSV file
excel_output.to_csv ((csv_output +"bdd_export.csv"), encoding='utf-8-sig', index=None)

如有任何帮助,我们将不胜感激。

干杯!

好的,我已经找到方法了。
只需添加:
df = pd.read_excel(file , usecols=lambda c: c in ['ref_01','ref_02', 'ref_03'])