简体   繁体   English

使用 Python 从 DB2 导出数据

[英]Exporting data from DB2 using Python

We have nearly 300 reports which we download from the DB2 database on daily basis in.xlsx format and circulate to many recipients.我们每天有近 300 份报告从 DB2 数据库下载,格式为.xlsx,并分发给许多收件人。 This is done using the Data Transfer utility in the IBM Package.这是使用 IBM Package 中的数据传输实用程序完成的。 It's a very time consuming task to download each and every file using the Data Transfer utility.使用数据传输实用程序下载每个文件是一项非常耗时的任务。 Is there a way I could download the data in.xlsx format by running a script in Python?有没有办法通过在 Python 中运行脚本来下载 .xlsx 格式的数据? All reports are already created so only requirement is to connect to the DB2 database library where the reports are saved and download the data in.xlsx format.所有报告都已创建,因此只需连接到保存报告的 DB2 数据库库并以.xlsx 格式下载数据。

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

You could use the Python module pandas.您可以使用 Python 模块 pandas。 There is a Stack Overflow answer showing how to load a Db2 result set into a pandas DataFrame .有一个堆栈溢出答案显示如何将Db2 结果集加载到 pandas DataFrame 中 pandas has the built-in function to_excel() which allows to generale an xlsx file. 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 . 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 .

So this is the very condensed code with the query itself missing:所以这是非常简洁的代码,查询本身缺失:

# 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