异常:异常('Exception caught in workbook destructor. Explicit close() may be required for workbook.',)

Exception: Exception('Exception caught in workbook destructor. Explicit close() may be required for workbook.',)

我正在尝试将一堆 CSV 文件放入一个工作簿中,这是我的代码:

import csv
import glob
import openpyxl
import os, sys
import pandas as pd
import xlsxwriter as xlwr

def main():

    list_of_files = []
    names = []
    for csv_file in glob.glob(os.path.join('.', '*.csv')):
        bleh = csv_file[2:]
        name = bleh[:-4]
        names.append(name)
        df = pd.read_csv(csv_file, index_col=None, header=0)
        list_of_files.append(df)

    writer = pd.ExcelWriter('non_concussed_game_logs.xlsx')
    for n, df in enumerate(list_of_files):
        df.to_excel(writer, '%s' % names[n])
    writer.save


if __name__ == "__main__":
    main()

我收到 post 标题中提到的错误,但我不确定为什么会收到它。我以前使用过这个脚本并且它有效,但我不确定为什么现在不行。感谢您的帮助!

我弄明白了,我的 CSV 文件是用 utf-8 编码的,所以我不得不进行 read_csv() 调用

df = pd.read_csv(csv_file, index_col=None, header=0, encoding='utf-8')

并将括号添加到 writer.save 行。