Adding progress monitor while inserting data to a Oracle db using Python

Advertisements

I would like to add a progress bar in my python script. I am running following lines of code. I am reading a file ‘MRSTY.RRF’ which is 0.195 MB. I would like to how much file has been processed.

insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
records=[]

with open("../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF" , 'r') as f:
    for line in f:
        line = line.strip()
        records.append(line.split("|"))
    
    for sublist in records:   #remove '' at the end of every element
        if sublist:
            sublist.pop()
    

for i in records:
    try:
        cur.execute(insert_sty,i)
        print ("record inserted")
        
    except Exception as e:
        print (i)
        print("Error: ",str(e))
        
conn.commit()

How can I achieve this?

>Solution :

You could do that with tqdm, it display a progress bar during long computations, and is very simple.

I have modified your code to add it.

from tqdm import tqdm

insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
records=[]

file_path = "../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF"
num_lines = sum(1 for line in open(file_path))

with open(file_path, 'r') as f:
    for line in tqdm(f, total=num_lines, desc="Processing file"):
        line = line.strip()
        records.append(line.split("|"))

    for sublist in records:  
        if sublist:
            sublist.pop()
    
for i in tqdm(records, desc="Inserting records"):
    try:
        cur.execute(insert_sty,i)
        print ("record inserted")       
    except Exception as e:
        print (i)
        print("Error: ",str(e))
        
conn.commit()

Leave a ReplyCancel reply