since it exceeds Excel’s limit of 65,530 URLS per worksheet
最近使用pandas做导出excel数据处理,导出数据为50万时,抛出了这个错误:since it exceeds Excel's limit of 65,530 URLS per worksheet.
1、代码:
import pandas as pd
# user_list = get_user_info(session)
# user_list是查询集列表
df = pd.DataFrame(user_list)
df = df.set_index('id')
df.to_excel('/Users/cuixin/Desktop/ceshi.xlsx')
print('ok!')
2、原因:这是由于Excel单个工作表限制URL类型数据量为65530,超出的部分会被舍弃
只要将strings_to_urls自动转换功能关闭就好了。
3、解决方案:
import pandas as pd
# user_list = get_user_info(session)
# user_list是查询集列表
df = pd.DataFrame(user_list)
df = df.set_index('id')
writer = pd.ExcelWriter(r'/Users/cuixin/Desktop/data.xlsx', engine='xlsxwriter', options={'strings_to_urls': False}) # 不将字符串转换为URL的选项创建ExcelWriter对象
df.to_excel(writer)
writer.close()
print('ok!')
备注:
URL类型数据指的是直接在excel表格中直接点击链接就能跳转到指定的网址。
非URL类型数据指的是就是一个字符串,不能在excel里直接点击打开。
pandas 写入excel 转换Url链接的两种方法
有时我们把pandas的DataFrame 写入excel时, 明明字符串是一个网址链接, 但是却不能在excel里直接点击打开, 有时却可以, 具体原因不详, 不过以下两个方法可以解决问题
第一种方法, 写一个函数, 把相应字段做转换
def excel_url_fun( input_value ):
''' 把一个网址字符串转换为excel公式 '''
return f'=HYPERLINK("{input_value}","{input_value}")'
if __name__ == "__main__":
df = pd.DataFrame({'url': ['https://www.baidu.com', 'https://www.163.com', 'https://www.csdn.net']})
df['link'] = '-'
df['link'] = df['url'].apply(lambda x: excel_url_fun(x))
df.to_excel('test.xlsx', index = False)
第二种方法, 使用 options={‘strings_to_urls’: True} 参数, 保存出来的xlsx文件里面如果有https://xxx
的url字符串, 就可以点击打开 , Python3.8.5 亲测通过
with pd.ExcelWriter("./datapd/focus.xlsx" , engine='xlsxwriter',options={'strings_to_urls': True}) as writer:
df.to_excel(writer, index=False)
来源:https://blog.csdn.net/qq_34663267/article/details/116129404
https://blog.csdn.net/majian/article/details/109734110