-9

I have to extract query results from an Oracle table as a comma delimited string in Python.

Thanks,

  • 1
    Have you tried anything yet? What exactly was the problem? – Mureinik Jan 11 '18 at 18:39
  • Im new to Python and I tried using many workarounds by googling but no luck. conn = ora.connect("HR", "oracle", "localhost/xe") cur=conn.cursor() cur.execute('select * from employees') s = ','.join([ r for r in cur.fetchall() ]) -- Tried this. Failed. – Boobal Ganesan Jan 11 '18 at 18:40
  • Answers would be helpful. Not down ratings :P – Boobal Ganesan Jan 11 '18 at 18:50

1 Answers1

0

Below codes will work, more details please refer to how to flatten a 2D list to 1D

"""
conn = ora.connect("HR", "oracle", "localhost/xe") 
cur=conn.cursor() 
cur.execute('select * from employees') 
dataset = cur.fetchall()
"""
dataset = [['1','FirstName1', 'LastName1', 'Department1'],
           ['2','FirstName2', 'LastName2', 'Department2']]
s = ','.join([item for row in dataset for item in row] )
print s
#output: 1,FirstName1,LastName1,Department1,2,FirstName2,LastName2,Department2
Sphinx
  • 10,163
  • 2
  • 24
  • 41