您的位置:首页技术文章
文章详情页

python - UTC格式转换成本地时间

【字号: 日期:2022-08-03 09:56:34浏览:29作者:猪猪

问题描述

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,你就该想到应该先把第一行表头排除掉。

标签: Python 编程
相关文章: