python - UTC格式转换成本地时间
问题描述
txt文件格式如下:area time data1 1383260400000 11.028366381681026453521 1383261000000 11.127100875673674451601 1383261600000 10.892770602791097545041 1383262200000 8.622424590989748338641 1383262800000 8.009927462445755708131 1383263400000 8.118419554089600254091 1383264000000 8.026269748512151025471 1383264600000 8.51417857718389292643······其中area的值从1到10000,time的值为UTC格式的时间,data为需要的数据现在想把time的时间改为本地时间,我Python新手一个,尝试写了一个程序,如下:
#!user/bin/env python3# -*- coding: gbk -*-import timefile = open(’day00.txt’, ’a+’)file.closefile = open(’day0.txt’,’r’)line = file.readline()time1 = [] #时间data1 = [] #dataarea = []while 1: line = file.readline() if line == ’’:break a = line.split() if a[0]==’area’:break if int(a[0]) == 1:area.append(a[0])time1.append(a[1])data1.append(a[2]) elif int(a[0]) < 10001:if a[0] not in area: area.append(a[0]) file1 = open(’day00.txt’, ’a+’) for i in time1:l_time = time.localtime(int(i)/1000)#ltime=time.localtime(1479285300)timeStr=time.strftime('%Y-%m-%d %H:%M:%S', l_time)file1.write('%-8s%-16s%.20fn' % (area[area.index(a[0])-1], timeStr, float(data1[time1.index(i)]))) file1.close file1 = open(’day00.txt’, ’r’) file1.close time1 = [] data1 = []else: time1.append(a[1]) data1.append(a[2]) else:breakfile.closefile = open(’day00.txt’, ’a+’)for j in time1: l_time=time.localtime(int(i)/1000) #ltime=time.localtime(1479285300) timeStr=time.strftime('%Y-%m-%d %H:%M:%S', l_time) file.write('%-8s%-16s%.20fn' % (a[0], timeStr, float(data1[time1.index(j)])))file.close# file = open(’day00.txt’, ’r’)# file.close
代码能跑通了在一位大神的帮助下已经解决啦,谢谢~
问题解答
回答1:学会看报错啊兄弟。这里说 area 无法转成 int,你就该想到应该先把第一行表头排除掉。
相关文章:
1. dockerfile - [docker build image失败- npm install]2. debian - docker依赖的aufs-tools源码哪里可以找到啊?3. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?4. docker绑定了nginx端口 外部访问不到5. docker容器呢SSH为什么连不通呢?6. 如何判断数组写入数据库有重复的值7. boot2docker无法启动8. dockerfile - 为什么docker容器启动不了?9. docker start -a dockername 老是卡住,什么情况?10. macos - mac下docker如何设置代理