我尝试写入Excel文件时收到错误消息。
在这一行:df_Percent_Change.to_excel(writer,sheet_name = x,startcol = 8)
回溯(最近通话最近): 文件“ /Users/david.soderstrom/Dropbox(Diagona)/DS/Python/Byggmarknad_index/Byggmarknad_index.py”,第125行,位于 df_Percent_Change.to_excel(writer,sheet_name = x,startcol = 8) 文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py”,行1766,在to_excel中 引擎=引擎) 写入文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/io/formats/excel.py”,第652行 Frozen_panes = freeze_panes) 在write_cells中的文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/pandas/io/excel.py”,行1742 wks = self.book.add_worksheet(sheet_name) 在add_worksheet中的第179行,文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py” 返回self._add_sheet(名称,worksheet_class = worksheet_class) _add_sheet中的文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py”,行666 名称= self._check_sheetname(名称,isinstance(工作表,图表)) _check_sheetname中的文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py”,第717行 如果len(sheetname)> 31: TypeError:“ int”类型的对象没有len() 异常在以下位置被忽略:> 追溯(最近一次通话): del 中的文件“ /Users/david.soderstrom/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py”,第154行 异常:工作簿析构函数中捕获了异常。工作簿可能需要显式close()。
# Calculate and save the percent change for each asset
if 'Percent_Change' not in excel_db.columns:
print('Percent_Change does not exist in excel file.')
print('Calculating...')
# Loop through and read each sheet
x = 0
for x in range(countSheets):
# Read in data for the calculation
data = pd.read_excel('databas.xlsx', sheet_name=x, index_col='Date')
# Calculate the percent change from day to day
Percent_Change = data['Adj Close'].pct_change()*100
print(type(Percent_Change))
df_Percent_Change = pd.DataFrame(Percent_Change)
print(type(df_Percent_Change))
writer = pd.ExcelWriter('databas.xlsx', engine='xlsxwriter')
df_Percent_Change.to_excel(writer, sheet_name=x, startcol=8)
# Save the result
writer.save()
writer.close()
x += 1