python写一个多线程读取并显示大型csv文件的exe工具
时间: 2023-08-14 12:06:03 浏览: 108
以下是一个Python多线程读取和显示大型CSV文件的例子:
```python
import csv
import threading
import time
def read_csv_chunk(filename, start, end):
with open(filename, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i >= start and i < end:
print(row)
def read_csv_multithreaded(filename, num_threads):
with open(filename, 'r') as f:
num_lines = sum(1 for line in f)
chunk_size = num_lines // num_threads
threads = []
for i in range(num_threads):
start = i * chunk_size
end = start + chunk_size
if i == num_threads - 1:
end = num_lines
t = threading.Thread(target=read_csv_chunk, args=(filename, start, end))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
if __name__ == '__main__':
start_time = time.time()
read_csv_multithreaded('large_file.csv', 4)
end_time = time.time()
print('Time taken:', end_time - start_time)
```
这个例子使用Python的CSV模块来读取CSV文件。它首先计算文件中的总行数,然后将文件分成多个块并在不同的线程中读取每个块。最后,它等待所有线程完成后输出所花费的总时间。
阅读全文