梦想照进现实的专栏

每天叫醒自己的不是闹钟,而是梦想

Python 函数封装

自己在项目中用到的几个函数封装


1.北京时间转换为世界时

def BTC_to_UTC(btc_time):
    utc_struct = time.mktime([int(btc_time[0:4]),int(btc_time[4:6]),\
                              int(btc_time[6:8]),int(btc_time[8:10]),\
                              int(btc_time[10:12]),int(btc_time[12:14]),0,0,0])
    
    return time.strftime('%Y%m%d%H%M%S',time.gmtime(utc_struct)) + '%03s' %(btc_time[14:])

2.按照日期生成以一分钟为间隔的序列

strTimeBegin = datetime.datetime.strptime(dataListFirst,'%Y-%m-%d')
        strTimeEnd = strTimeBegin + datetime.timedelta(days = 1)
        strTimeMi = datetime.timedelta(minutes = 1)
        
        
        while strTimeBegin < strTimeEnd:
            dataListItem = [strTimeBegin.strftime('%Y-%m-%d'), strTimeBegin.strftime('%H:%M:%S.%f')[0:-3]]
            returnDataList.append(dataListItem)
            strTimeBegin = strTimeBegin + strTimeMi

3.平均值计算

for i in range(lenreDataList):
            if i < len(returnDataList) - 1:
                timeBegin = returnDataList[i][1]
                timeEnd = returnDataList[i + 1][1]
            else:
                timeBegin = returnDataList[i][1]
                timeEnd = '23:59:59:999'
                
            dataListTemp = []
            for j in range(lenDataList):
                if dataList[j][1] > timeBegin and dataList[j][1] < timeEnd:
                    dataListTemp.append(dataList[j])
            
            for e in avgList:
                iCount = 0.0
                addValue = 0.0
                for k in range(len(dataListTemp)):
                    if dataListTemp[k][e] != self.errorValue:
                        addValue += float(dataListTemp[k][e])
                        iCount += 1.0
                if iCount != 0.0:
                    avgValue = '%.2e' %(addValue / iCount)
                else:
                    avgValue = -999.9
                returnDataList[i].append(avgValue)
        return returnDataList          


阅读更多
文章标签: python struct float
个人分类: python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭