Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法。分享给大家供大家参考。具体实现方法如下:

# Export Oracle database tables to CSV files
# FB36 - 201007117
import sys
import csv
import cx_Oracle
connection = raw_input("Enter Oracle DB connection (uid/pwd@database) : ")
orcl = cx_Oracle.connect(connection)
curs = orcl.cursor()
printHeader = True # include column headers in each table output
sql = "select * from tab" # get a list of all tables
curs.execute(sql)
for row_data in curs:
  if not row_data[0].startswith('BIN$'): # skip recycle bin tables
    tableName = row_data[0]
    # output each table content to a separate CSV file
    csv_file_dest = tableName + ".csv"
    outputFile = open(csv_file_dest,'w') # 'wb'
    output = csv.writer(outputFile, dialect='excel')
    sql = "select * from " + tableName
    curs2 = orcl.cursor()
    curs2.execute(sql)
    if printHeader: # add column headers if requested
      cols = []
      for col in curs2.description:
        cols.append(col[0])
      output.writerow(cols)
    for row_data in curs2: # add table rows
      output.writerow(row_data)
    outputFile.close()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python3结合openpyxl库实现excel操作的实例代码

一.相关说明: 1、openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件;2007一下的版本为xls结尾的文件,需要使用 xlrd和xlwt库进行...

Python3使用requests发闪存的方法

requests是一个python 轻量的http客户端库,相比python的标准库要优雅很多。接下来通过本文给大家介绍Python3使用requests发闪存的方法,一起学习吧。 使...

在pytorch 中计算精度、回归率、F1 score等指标的实例

pytorch中训练完网络后,需要对学习的结果进行测试。官网上例程用的方法统统都是正确率,使用的是torch.eq()这个函数。 但是为了更精细的评价结果,我们还需要计算其他各个指标。在...

python使用pandas实现数据分割实例代码

本文研究的主要是Python编程通过pandas将数据分割成时间跨度相等的数据块的相关内容,具体如下。 先上数据,有如下dataframe格式的数据,列名分别为date、ip,我需要统计...

Python正则表达式实现截取成对括号的方法

本文实例讲述了Python正则表达式实现截取成对括号的方法。分享给大家供大家参考,具体如下: strs = '1(2(3(4(5(67)6)7)8)9)0' reg1 = re.co...