问题描述
||
我需要将oracle表导出为csv / excel文件格式(以及列标题)。通过cx_oracle或sqlplus welcome的解决方案。
注释中的Python代码:
con = cx.connect()
cur = con.cursor()
printer = cur.execute(sqlcode)
con.commit()
解决方法
也许使用csv模块(来自标准库):
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()展开为循环。
快乐的足迹!
,要创建XLS文件,请使用python-excel项目中的xlwt模块。
,对于非Python解决方案,Tom Kyte具有一些出色的脚本,可使用PL / SQL(UTL_FILE),SQL * Plus和Pro * C生成CSV文件。