python2.7小程序练手源码 多线程读取文件夹下的多个文件进行数据处理生成Excel

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
import time
import os
import uuid
import xlwt     #导入xlwt

# 首先获得Iterator对象:
#it = iter([x for x in range(1,20)])
root_path = "e:/demo"
target_path = "e:/demo/result"
files = os.listdir(root_path)
file_list = []
for f in files:
    if os.path.isfile(root_path + '/' + f):
        file_list.append(root_path + '/' + f)
# 获得Iterator对象:        
it = iter(file_list)
class myThread(threading.Thread):
    def __init__(self,threadId,name,target):
        threading.Thread.__init__(self)
        self.threadId = threadId
        self.name = name
        self.target = target
    def run(self):
        print u"开始线程:",self.name
        # 获得锁,成功获得锁定后返回 True
        # 可选的timeout参数不填时将一直阻塞直到获得锁定
        # 否则超时后将返回 False
        # 循环:
        while True:
            try:
                threadLock.acquire()
                # 获得下一个值:
                x = next(self.target)
                print "%s : %s" % (self.name,x)
                # 释放锁
                threadLock.release()
                write_excel(x,target_path + '/' + str(uuid.uuid1()) + '.xls' )
            except StopIteration:
                # 遇到StopIteration就退出循环
                threadLock.release()
                break
        
    def __del__(self):
        print self.name,u"线程结束!"
def print_time(threadName,delay,counter):
    while counter:
        time.sleep(delay)
#读取文件生成处理后的文件
def write_excel(filePath,targetPath):
    
    f0 = xlwt.Workbook()
    sheet1 = f0.add_sheet('Sheet1',cell_overwrite_ok=True)
    result=""
    index=0     #行索引
    t_index = 0
    titles = [u'本地视频码率',u'本地音频码率',u'远端视频码率',u'远端音频码率',u'本地帧率',u'本地宽度分辨率输出',u'本地高度分辨率输出']
    params = ['_lv_nBytesSent','_la_nBytesSent','_rv_nBytesReceived','_ra_nBytesReceived','_lv_nFrameRateSent','_lv_nFrameWidthSent','_lv_nFrameHeightSent']
    for t in titles:     #写标题头
        sheet1.write(0, t_index, t)
        t_index = t_index + 1
    with open(filePath) as f:
        for line in f:
            if '-------------------------------------' in line:
                index = index + 1
                continue
            for p in range(len(params)):
                if params[p] in line:
                    result = line.split("=")[1].replace("\n","").strip()
                    sheet1.write(index,p,float(result))
                    break
    f0.save(targetPath)       
threadLock = threading.Lock()
threads = []
# 创建新线程
thread1 = myThread(1,"Thread-1",it)
thread2 = myThread(2,"Thread-2",it)
thread3 = myThread(3,"Thread-3",it)
# 开启新线程
thread1.start()
thread2.start()
thread3.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
# 等待所有线程完成
for t in threads:
    t.join()
print u"主进程结束!"

 

你可能感兴趣的:(python练手源码)