Python Forum

Full Version: Query output in Tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, I have this wuery output that I want to write to a text file. But it is writeen with "(" and "'" and I want it clear.

From database I have this output:

NAME
------------------------------
DATA
RECO

My python code is like this:
import cx_Oracle as oracledb
import xml.etree.ElementTree as ET
conn = oracledb.connect("/", mode=oracledb.SYSASM)
#prev_name = None
cursor = conn.cursor()
cursor.execute("select distinct name from v$asm_diskgroup")
for dgname in cursor:
    xml_data_cell=dgname
    print(dgname)
    print(type(dgname))
Output from python is :
('DATAC3',)
<type 'tuple'>
('RECOC3',)
<type 'tuple'>
>>>
And text file generated is :
cat /tmp/dgnames
('DATA',)('RECO',)
I want this file with something like:
DATA
RECO

I don't knwo if I need to convert it to string. I tried using STR but didn't work.
the query result is consists of one-element tuples. You can always access the first element in each tuple by dgname[0], or create a list with all first elements

spam=(('DATA',),('RECO',))
eggs = next(zip(*spam)) # eggs = list(zip(*spam))[0]
print(eggs)
print('\n'.join(eggs))
(Apr-07-2022, 12:31 PM)buran Wrote: [ -> ]the query result is consists of one-element tuples. You can always access the first element in each tuple by dgname[0], or create a list with all first elements

spam=(('DATA',),('RECO',))
eggs = next(zip(*spam)) # eggs = list(zip(*spam))[0]
print(eggs)
print('\n'.join(eggs))

Thanks a lot, I'm so new to this :)