繁体   English   中英

使用 Python 从 DB2 导出数据

[英]Exporting data from DB2 using Python

我们每天有近 300 份报告从 DB2 数据库下载,格式为.xlsx,并分发给许多收件人。 这是使用 IBM Package 中的数据传输实用程序完成的。 使用数据传输实用程序下载每个文件是一项非常耗时的任务。 有没有办法通过在 Python 中运行脚本来下载 .xlsx 格式的数据? 所有报告都已创建,因此只需连接到保存报告的 DB2 数据库库并以.xlsx 格式下载数据。

用于从 DB2 下载数据的实用程序

您可以使用 Python 模块 pandas。 有一个堆栈溢出答案显示如何将Db2 结果集加载到 pandas DataFrame 中 pandas 具有内置的 function to_excel()允许通用 xlsx 文件。

If you want to run the Python script on the Db2 server but download it to a local machine, you can take a look into my blog how to use the function to generate in-memory Excel or CSV files .

所以这是非常简洁的代码,查询本身缺失:

# read the Db2 result set into a DataFrame
df = pd.read_sql(sql, conn)
# write out to Excel file
df.to_excel('your_data.xslx', sheet_name='Sheet1')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM