我目前正在处理以下代码,该代码采用生成的CSV文件并将其转换为XLSX文件。它大部分时间都有效,但出于某种原因它会失败,比如每运行5到6次就会失败。知道我做错了吗?
df = pd.read_csv(path_... encoding = "ISO-8859-1")
new_headers = ['Header 1', 'Header 2', ... 'Header N']
writer = pd.ExcelWriter(path,engine='xlsxwriter')
i = 0
while i < len(new_headers):
df.columns.values[i] = new_headers[i]
i = i + 1
df.to_excel(writer, header=True, start index=False, encoding='UTF-8')
writer.save()
我考虑过进行以下更改,但在测试之后我确信它无效 -
df = pd.read_csv(path_... encoding = "ISO-8859-1")
new_headers = ['Header 1', 'Header 2', ... 'Header N']
writer = pd.ExcelWriter(path,engine='xlsxwriter')
df.columns = new_headers
df.to_excel(writer, header=True, start index=False, encoding='UTF-8')
writer.save()
我还读到添加startcol = 0和startrow = 0会停止发生某些自动格式化,这可能是此问题的根源。这可能是问题所在,还是有其他问题?