从Unix时间戳到ISO时间戳的时间转换

时间:2016-05-21 15:42:01

标签: python postgresql timestamp

所以这是我的代码:

import psycopg2
import datetime

conn = psycopg2.connect(database="sample", user="postgres", password="", host="localhost", port="5432")
cur = conn.cursor()

print "Opened database successfully"
ii=0
with open ('sms-call-internet-tn-2013-11-01.txt') as f:
    for line in f:
        print line;
        arr= line.split('\t');
        square_id=[0];
        time_interval=datetime.datetime.utcfromtimestamp(int(arr[1]).strftime('%Y-%m-%d %H:%M:%S'));
        country_id=index[2];
        smsin=arr[3];
        if arr[3]==" ":
            arr[3]="0"
        smsout=arr[4];
        if arr[4]==" ":
            arr[4]="0"

        callin=arr[5];
        if arr[5]==" ":
            arr[5]="0"

        callout=arr[6];
        if arr[6]==" ":
            arr[6]="0"

        internet=arr[7]   
        if arr[7]==" ":
            arr[7]="0"

        cur.execute ("INSERT INTO tn2013_12_02 VALUES (" + square_id + ", " + time_interval + ", " + country_id + ", " + smsin + ", " + smsout + ", '" + callin + "', "+ callout +", "+  internet +")");

        conn.commit()
        ii= ii+1;

这是错误:

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
AttributeError: 'long' object has no attribute 'strftime'

1 个答案:

答案 0 :(得分:0)

你的括号错了。改变这个:

datetime.datetime.utcfromtimestamp(int(arr[1]).strftime('%Y-%m-%d %H:%M:%S'))

到此:

datetime.datetime.utcfromtimestamp(int(arr[1])).strftime('%Y-%m-%d %H:%M:%S')