python - UTC格式转换成本地时间
大家讲道理
大家讲道理 2017-04-18 10:32:55
0
1
530

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

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
大家讲道理

Learn to read error reports, brother. It says here that area cannot be converted to int, so you should think of excluding the first row header first.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!