我正在使用以下代码将数据框复制到 Excel 文档中:
dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol)
Run Code Online (Sandbox Code Playgroud)
问题是我收到以下错误,因为 URL 太长:
Ignoring URL .... 255 characters since it exceeds Excel's limit for URLS
Run Code Online (Sandbox Code Playgroud)
所以,我找到了一个使用的解决方案:
dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol, options={'strings_to_urls': False})
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
TypeError: to_excel() got an unexpected keyword argument 'options'
Run Code Online (Sandbox Code Playgroud)
显然 options 不适用于to_excel,这是我在脚本中用于许多其他代码的内容(即我想坚持使用 'to_excel')
我的问题有什么解决方案吗?
这不是重复:How to save in *.xlsx long URL in cell using Pandas as that is not about to_excel (it is about excelwriter)
Myk*_*nin 10
传递参数options={'strings_to_urls': False}给pd.ExcelWriter创建时调用writer。它看起来像这样:
writer = pd.ExcelWriter('C:/Users/{}/Desktop/RF Data.xlsx'.format(staName2),options={'strings_to_urls': False})
dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol)
Run Code Online (Sandbox Code Playgroud)
你可以使用:
with pd.ExcelWriter(file_name, options={'strings_to_urls': False}) as writer:
df.to_excel(writer, 'Influence on Policy', columns=colsPol)
Run Code Online (Sandbox Code Playgroud)
那么你就不必担心关闭你的 writer 文件