Writing Oracle Output to a File

t_rectenwald t.rectenwald at gmail.com
Wed Dec 26 10:36:39 EST 2007


Hello,

I attempting to execute an Oracle query, and write the results to a
file in CSV format.  To do so, I've done the following:

import cx_Oracle
db = cx_Oracle.connect('user/pass at DBSID')
cursor = db.cursor()
cursor.arraysize = 500
cursor.execute(sql)
result = cursor.fetchall()

The above works great.  I'm able to connect to the database and print
out the results as a list of tuples.  Here is where I get lost.  How
do I work with a "list of tuples?"  My understanding is that a "list"
is basically an array (I don't come from a Python background).  Tuples
are a "collection of objects."  So, if I do...

print result[0]

I get the first row of the query, which would make sense.  The problem
is that I cannot seem to write tuples to a file.  I then do this...

csvFile = open("output.csv", "w")
csvFile = write(result[0])
csvFile.close

This generates an exception:

TypeError: argument 1 must be string or read-only character buffer,
not tuple

So, I'm a bit confused as to the best way to do this.  I guess I could
try to convert the tuples into strings, but am not sure if that is the
proper way to go.  Any help would be appreciated.  I've also seen a
csv module out there, but am not sure if that is needed in this
situation.

Best Regards,
Tom



More information about the Python-list mailing list