关闭

Python 函数封装

663人阅读 评论(0) 收藏 举报

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


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          


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:97711次
    • 积分:1660
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:40篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论