Python脱离索引错误

时间:2013-11-01 17:32:24

标签: python datetime twitter

我正在努力将Twitter created_at日期转换为整数并使用以下代码,但我得到的是索引错误:

creation_date = [time.strftime('%Y-%m-%d %H:%M:%S',time.strptime(status['created_at']
                  ,'%a %b  %d %H:%M:%S +0000 %Y')) for status in statuses]

for x in range(len(creation_date)):
    year = int(creation_date[x][0:4])
    month = int(creation_date[x][5:7])
    day = int(creation_date[x][8:10])
    newCreationDate = []
    newCreationDate[x] = datetime(year,month,day)

2 个答案:

答案 0 :(得分:1)

您每次循环时都会清空newCreationDate数组。

newCreationDate = []
for d in creation_date:
    year = int(d[0:4])
    month = int(d[5:7])
    day = int(d[8:10])
    newCreationDate.append(datetime(year,month,day))

答案 1 :(得分:1)

试试这个:

newCreationDate = []
for x in range(len(creation_date)):
    year = int(creation_date[x][0:4])
    month = int(creation_date[x][5:7])
    day = int(creation_date[x][8:10])
    newCreationDate.append(datetime(year,month,day))