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

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

我怎样才能做到这一点呢?

推荐答案

你可以用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()

Python相关问答推荐

调试回归无法解决我的问题

键盘.任务组

如何观察cv2.erode()的中间过程?

在Windows上启动新Python项目的正确步骤顺序

symy.分段使用numpy数组

如何使用上下文管理器创建类的实例?

在Python中为变量的缺失值创建虚拟值

提取两行之间的标题的常规表达

更改matplotlib彩色条的字体并勾选标签?

acme错误-Veritas错误:模块收件箱没有属性linear_util'

'discord.ext. commanders.cog没有属性监听器'

scikit-learn导入无法导入名称METRIC_MAPPING64'

在单个对象中解析多个Python数据帧

计算分布的标准差

手动设置seborn/matplotlib散点图连续变量图例中显示的值

人口全部乱序 - Python—Matplotlib—映射

python sklearn ValueError:使用序列设置数组元素

如何按row_id/row_number过滤数据帧

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

如何将一组组合框重置回无 Select tkinter?