Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cursor write 3rd file empty
#1
Hi all,

I have this code below where from table all have values so all files generated should not be empty.

import cx_Oracle as oracledb

def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.CLOB:
        return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize)
    if default_type == oracledb.BLOB:
        return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)

conn = oracledb.connect("/", mode=oracledb.SYSASM)
conn.outputtypehandler = output_type_handler

prev_name = None
cursor.execute("select cellname, confval from v$cell_config where conftype='CELL' order by cellname desc")
for name, val in cursor:
    if name != prev_name:
        filename = "my_file_%s.xml" %name
        print("File name is: ", filename) 
        print("XML Content for this line is: ", val) 
        f = open(filename,"w")
        prev_name = name
        f.write(val)
It generate 3 files where 1 is empty:

Output:
-rw-r--r-- 1 grid oinstall 2008 Mar 9 23:23 my_file_172.16.4.129;172.16.4.130.xml -rw-r--r-- 1 grid oinstall 2008 Mar 9 23:23 my_file_172.16.4.127;172.16.4.128.xml -rw-r--r-- 1 grid oinstall 0 Mar 9 23:23 my_file_172.16.4.125;172.16.4.126.xml
If I debug using print I can see data for all 3 lines .

Seems cursor is writing X-1 (Skeeping the latest one it tries to generate. It create file but did not write to it.)

Can anyone help on this?
Yoriz write Mar-09-2022, 11:34 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Close your files.

A few comments about your code.

Are there only two possible types?
def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.CLOB:
        return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize)
    if default_type == oracledb.BLOB:
        return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)
If so, you should write like this:
def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.CLOB:
        return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize)
   return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)
If there are more types than CLOB and BLOB, you should raise an exception
def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.CLOB:
        return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize)
    if default_type == oracledb.BLOB:
        return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)
    raise ValueError("Unsupported output type")
Functions should never return a value, always return a value, or throw an error if it is supposed to return something but can't. This prevents the confusing error message:
Error:
AttributeError: 'NoneType' object has no attribute xxx
Is this code supposed to prevent duplicates?
for name, val in cursor:
    if name != prev_name:
It will if the replay is sorted by name. Is that the case? Is that always the case?

And be sure to close your files.
for name, val in cursor:
    if name != prev_name:
        filename = "my_file_%s.xml" %name
        print(f"File name is: {filename}\nXML Content for this line is: {val}") 
        with open(filename, "w") as file:  # Context manager automatically closes file for you
            file.write(val)
        prev_name = name
Reply
#3
It is showing errors like below:
>>> cursor = conn.cursor()
>>> cursor.execute("select cellname, confval from v$cell_config where conftype='CELL' order by cellname desc")
<cx_Oracle.Cursor on <cx_Oracle.Connection to user @local>>
>>> for name, val in cursor:
...     if name != prev_name:
...         filename = "my_file_%s.xml" %name
...         print(f"File name is: {filename}\nXML Content for this line is: {val}")
Error:
File "<stdin>", line 4 print(f"File name is: {filename}\nXML Content for this line is: {val}") ^ SyntaxError: invalid syntax >>> with open(filename, "w") as file: # Context manager automatically closes file for you File "<stdin>", line 1 with open(filename, "w") as file: # Context manager automatically closes file for you ^ IndentationError: unexpected indent >>> file.write(val) File "<stdin>", line 1 file.write(val) ^ IndentationError: unexpected indent >>> prev_name = name File "<stdin>", line 1 prev_name = name ^ IndentationError: unexpected indent

I'm using python 2.7. Seems it does not work with print(f) . I remove and now is working fine. tks
Reply
#4
(Mar-10-2022, 01:42 PM)paulo79 Wrote: I'm using python 2.7. Seems it does not work with print(f) . I remove and now is working fine. tks

from __future__ import print_function
https://docs.python.org/3/library/__future__.html

Support since Python 2.6.0a2, but if you import the print_function, you've to change all existing code where print is used.
paulo79 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upgrading from 2 to 3 and having file write problems KenHorse 2 454 May-08-2022, 09:47 PM
Last Post: KenHorse
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 327 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  trying to write a dictionary in a csv file CompleteNewb 13 961 Mar-04-2022, 04:43 AM
Last Post: deanhystad
  CSV to Text File and write a line in newline atomxkai 4 827 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  Strange write()/File behavior kaega2 2 566 Jan-28-2022, 02:53 AM
Last Post: kaega2
Photo how I write the output into xml file in python? 3lnyn0 1 859 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  rows from sql query need to write to a file as columns sjcsvatt 6 1,143 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Can't write to file Mark17 2 955 Sep-24-2021, 03:45 PM
Last Post: Mark17
  How to write in text file - indented block Joni_Engr 3 1,387 Aug-09-2021, 06:30 PM
Last Post: deanhystad
  Read and write active Excel file euras 4 1,472 Jun-29-2021, 11:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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