写在前面
在实际情况中,若通过Pandas读取较大文件时,个人认为常用的progressbar,progress,tqdm包都无法较好的实现进度显示问题。所以自己尝试些了一段代码来实现。
- 需要导入的包如下
import threading
import time
import sys
import pandas as pd
from threading import Thread
- 建立Pandas读取文件的方法
def read():
return pd.read_csv('d:/hz4.csv', encoding='gbk'
'')
- 建立显示Spinner的方法
def progress(thread_name):
letters = '|\\-|/-'
i = 0
is_alive = True
start = time.time()
while is_alive:
sys.stdout.write('Loading file.' + letters[i % 5] + '\b' * 20)
sys.stdout.flush()
time.sleep(0.5)
i += 1
is_alive = thread_name.is_alive()
end = time.time()
elapsed_time = '%.2f' % (end - start)
sys.stdout.write('Loading finished. Elapsed time: ' + str(elapsed_time) + " Seconds")
- 通过threading调用
if __name__ == '__main__':
read_thread = Thread(target=read)
progress_thread = Thread(target=progress, args=(read_thread,))
read_thread.start()
progress_thread.start()
read_thread.join()
progress_thread.join()
网友评论