Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query output in Tuple
#1
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.
Reply
#2
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,847 Nov-04-2020, 11:26 AM
Last Post: Aggam
  str.endswith Output Query eddywinch82 8 3,863 Aug-21-2020, 11:59 PM
Last Post: eddywinch82
  Format SQL Query Output phillyfa 2 4,069 Apr-22-2020, 07:45 AM
Last Post: buran
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,218 May-21-2019, 11:39 AM
Last Post: avorane
  Sqlalchemy Query into tuple KirkmanJ 0 2,473 Jul-10-2018, 02:56 PM
Last Post: KirkmanJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020