如何将表格导出为CSV或Excel格式 [英] How to export a table to csv or excel format

查看:64
本文介绍了如何将表格导出为CSV或Excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将oracle表导出为csv/excel文件格式(以及列标题).通过cx_oracle或sqlplus Welcome的解决方案.

I need to export a oracle table to a csv/excel file format (along with the column headings). A solution through cx_oracle or through sqlplus welcome.

注释中的Python代码:

Python code from the comment:

con = cx.connect() 
cur = con.cursor() 
printer = cur.execute(sqlcode) 
con.commit() 

推荐答案

也许使用csv模块(来自标准库):

perhaps use csv module (from standard library):

import csv
cursor = connection.cursor() # assuming you know how to connect to your oracle db
cursor.execute('select * from table_you_want_to_turn_to_csv')
with open('output_file.csv', 'wb') as fout:
    writer = csv.writer(fout)
    writer.writerow([ i[0] for i in cursor.description ]) # heading row
    writer.writerows(cursor.fetchall())

如果有大量数据,请将fetchall()展开为循环.

If you have loads of data, unroll the fetchall() into a loop.

快乐的足迹!

这篇关于如何将表格导出为CSV或Excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆