【问题标题】:(PYTHON) to_excel - Ignoring URL... 255 characters since it exceeds Excel's limit for URLS(PYTHON) to_excel - 忽略 URL...255 个字符,因为它超出了 Excel 对 URLS 的限制
【发布时间】:2018-03-27 03:32:28
【问题描述】:

我正在使用以下代码将数据框复制到 Excel 文档中:

dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol)

问题是我收到以下错误,因为 URL 太长:

Ignoring URL .... 255 characters since it exceeds Excel's limit for URLS

所以,我找到了一个解决方案:

dfPol.to_excel(writer, 'Influence on Policy', columns=colsPol, options={'strings_to_urls': False})

但我收到以下错误:

TypeError: to_excel() got an unexpected keyword argument 'options'

显然选项不适用于to_excel,这是我在脚本中的许多其他代码中使用的(即我想坚持使用“to_excel”)

我的问题有什么解决办法吗?

这不是 How to save in *.xlsx long URL in cell using Pandas 的副本,因为这不是关于 to_excel(它是关于 excelwriter)

【问题讨论】:

  • 你试过用strings_to_urls作为参数吗?
  • options 是作者的参数,而不是 to_excel 函数
  • 所以,将options={'strings_to_urls': False} 传递给pd.ExcelWriter 调用
  • @ScoutEU,发布,谢谢,祝你好运

标签: python pandas


【解决方案1】:

你可以使用:

 with pd.ExcelWriter(file_name, options={'strings_to_urls': False}) as writer:
        df.to_excel(writer, 'Influence on Policy', columns=colsPol)

那么你就不用担心关闭你的 writer 文件了

【讨论】:

    【解决方案2】:

    在创建writer 时将参数options={'strings_to_urls': False} 传递给pd.ExcelWriter 调用。 它看起来像这样:

    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)
    

    【讨论】:

    • 在我加入 writer.close() 之前,上面的代码对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多