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

huangapple go评论95阅读模式
英文:

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

问题

我想在我的Python脚本中添加一个进度条。我正在运行以下代码行。我正在读取一个名为'MRSTY.RRF'的文件,其大小为0.195 MB。我想知道已处理了多少文件。

  1. insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
  2. records=[]
  3. with open("../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF" , 'r') as f:
  4. for line in f:
  5. line = line.strip()
  6. records.append(line.split("|"))
  7. for sublist in records: #remove '' at the end of every element
  8. if sublist:
  9. sublist.pop()
  10. for i in records:
  11. try:
  12. cur.execute(insert_sty,i)
  13. print ("record inserted")
  14. except Exception as e:
  15. print (i)
  16. print("Error: ",str(e))
  17. conn.commit()

如何实现这一目标?

英文:

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.

  1. insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
  2. records=[]
  3. with open("../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF" , 'r') as f:
  4. for line in f:
  5. line = line.strip()
  6. records.append(line.split("|"))
  7. for sublist in records: #remove '' at the end of every element
  8. if sublist:
  9. sublist.pop()
  10. for i in records:
  11. try:
  12. cur.execute(insert_sty,i)
  13. print ("record inserted")
  14. except Exception as e:
  15. print (i)
  16. print("Error: ",str(e))
  17. conn.commit()

How can I achieve this?

答案1

得分: 2

你可以使用 tqdm 来实现这一点,它可以在长时间计算过程中显示进度条,非常简单。

我已经修改了你的代码来加入它。

  1. from tqdm import tqdm
  2. insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
  3. records=[]
  4. file_path = "../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF"
  5. num_lines = sum(1 for line in open(file_path))
  6. with open(file_path, 'r') as f:
  7. for line in tqdm(f, total=num_lines, desc="Processing file"):
  8. line = line.strip()
  9. records.append(line.split("|"))
  10. for sublist in records:
  11. if sublist:
  12. sublist.pop()
  13. for i in tqdm(records, desc="Inserting records"):
  14. try:
  15. cur.execute(insert_sty,i)
  16. print ("record inserted")
  17. except Exception as e:
  18. print (i)
  19. print("Error: ",str(e))
  20. conn.commit()
英文:

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.

  1. from tqdm import tqdm
  2. insert_sty = "insert into MRSTY (CUI,TUI,STN,STY,ATUI,CVF) values (:0,:1,:2,:3,:4,:5)"
  3. records=[]
  4. file_path = "../umls_files/umls-2023AA-metathesaurus-full/2023AA/META/MRSTY.RRF"
  5. num_lines = sum(1 for line in open(file_path))
  6. with open(file_path, 'r') as f:
  7. for line in tqdm(f, total=num_lines, desc="Processing file"):
  8. line = line.strip()
  9. records.append(line.split("|"))
  10. for sublist in records:
  11. if sublist:
  12. sublist.pop()
  13. for i in tqdm(records, desc="Inserting records"):
  14. try:
  15. cur.execute(insert_sty,i)
  16. print ("record inserted")
  17. except Exception as e:
  18. print (i)
  19. print("Error: ",str(e))
  20. conn.commit()

huangapple
  • 本文由 发表于 2023年5月29日 04:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定