Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

UTC格式转换成本地时间

txt文件格式如下:
area time data
1 1383260400000 11.02836638168102645352
1 1383261000000 11.12710087567367445160
1 1383261600000 10.89277060279109754504
1 1383262200000 8.62242459098974833864
1 1383262800000 8.00992746244575570813
1 1383263400000 8.11841955408960025409
1 1383264000000 8.02626974851215102547
1 1383264600000 8.51417857718389292643
······
其中area的值从1到10000,time的值为UTC格式的时间,data为需要的数据
现在想把time的时间改为本地时间,我Python新手一个,尝试写了一个程序,如下:

#!user/bin/env python3
# -*- coding: gbk -*-
import time
file = open('day00.txt', 'a+')
file.close
file = open('day0.txt','r')
line = file.readline()
time1 = []  #时间
data1 = []  #data
area = []

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%.20f
" % (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:
        break
file.close

file = 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%.20f
" % (a[0], timeStr, float(data1[time1.index(j)])))
file.close
# file = open('day00.txt', 'r')
# file.close

代码能跑通了
在一位大神的帮助下已经解决啦,谢谢~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

学会看报错啊兄弟。这里说 area 无法转成 int,你就该想到应该先把第一行表头排除掉。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...