使用Python向Oracle数据库插入数据时添加进度监视器

wz3gfoph  于 2023-06-05  发布在  Oracle
关注(0)|答案(1)|浏览(396)

我想在我的python脚本中添加一个进度条。我正在运行以下代码行。我正在阅读一个文件'MRSTY.RRF'这是0.195 MB。我想知道已经处理了多少文件。

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()

我如何才能做到这一点?

nr7wwzry

nr7wwzry1#

你可以用tqdm来做,它在长时间的计算中显示一个进度条,非常简单。
我修改了你的代码来添加它。

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()

相关问题