zoukankan      html  css  js  c++  java
  • python 抽取数据

    # -*- coding: utf-8 -*-
    import cx_Oracle
    from pprint import pprint
    import csv
    import time
    import re
    import binascii
    
    conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
    cursor = conn.cursor()
    table_name="t300"
    owner="system"
    f = open(table_name+".csv", "w")
    writer = csv.writer(f, lineterminator="
    ", quoting=csv.QUOTE_NONNUMERIC)
    xsql="select COLUMN_NAME from dba_tab_columns where table_name=upper("+"'"+table_name+"'"+")and owner=upper("+"'"+owner+"'"+")"+" "+"order by column_id asc"
    print xsql
    r = cursor.execute(xsql)
    columns=[];
    for (col,) in r:
        print col
        columns.append(col)
    
    ysql="select COLUMN_NAME,DATA_TYPE from dba_tab_columns where table_name=upper("+"'"+table_name+"'"+")and owner=upper("+"'"+owner+"'"+")"+" "+"order by column_id asc"
    d={}
    r = cursor.execute(ysql)
    m=0
    arr1=[]
    for j in r:
        #print j
            if (j[1]=='RAW'):
                print j[1]
                arr1.append(m)
            else:
                pass
            m = m + 1
    print arr1
    print '------------------'
    exportOracleSql="SELECT ";
    for i in range(0, len(columns)):
        if i != len(columns) - 1:
            exportOracleSql=exportOracleSql+columns[i]+','
        else:
            exportOracleSql = exportOracleSql + columns[i]
    exportOracleSql=exportOracleSql+" "+"from"+" "+owner+"."+table_name
    print exportOracleSql
    cursor.execute("alter session set nls_date_format='yyyy-mm-dd'")
    s1 = cursor.execute(exportOracleSql)
    for row in s1:
        #print type(row)
        s2=[]
        for i in range(0,len(row)):
             if (i in arr1):
                 #print (binascii.b2a_hex(row[i]))
                 s2.append(binascii.b2a_hex(row[i]))
                 #writer.writerow( binascii.b2a_hex(row[i]))
             else:
                  #print row[i]
                  s2.append(row[i])
                 #writer.writerow(row[i])
        print s2
        writer.writerow(s2)
    f.close()
    cursor.close()
    close__ = conn.close()
    

  • 相关阅读:
    db2中的常用命令及使用方法
    互联网服务应用协议设计
    Zookeeper整理(一)- 写操作产生事件,写操作与Watcher对应关系
    实现自己的连接池(一)
    SMP、NUMA、MPP体系结构介绍
    随手备忘 ubuntu12.04 lts 安装gcc 4.8
    认真体会 结构体中的零长度数组
    由内存池实现总结内存对齐问题
    认真理解 oom killer 备忘
    仔细体会 epoll中的et lt模式
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349571.html
Copyright © 2011-2022 走看看