0

我正在将 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)
4

2 回答 2

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))
于 2013-11-01T17:39:28.330 回答
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))
于 2013-11-01T17:35:50.573 回答