遍历每个列表

时间:2013-11-28 12:43:08

标签: python list date loops

我为从日志文件读取的每个输入行生成了一行列表 我想遍历每个列表并解析列表中的日期属性位,请提供您的建议

listgen  = line.split() 

['2013-07-23T14:08:17.111906+01:00', 'server24', 'status=inprogress', 'MSG= Transfer Intitated']
['2013-07-23T14:08:26.630043+01:00', 'server x2', 'status=waiting', 'MSG= waiting for response']
['2013-07-23T14:08:27.188360+01:00', 'server35', 'status=waiting', 'MSG= waiting for response']
['2013-07-23T14:08:27.306438+01:00', 'server35x', 'status=successful', 'MSG= Transfer Completed']
['2013-07-23T14:08:27.342922+01:00', 'serverx56', 'status=inprogress', 'MSG= Transfer Intitated']
['2013-07-23T15:12:10.803993+01:00', 'server267', 'status=waiting', 'MSG= waiting for response']

我想遍历每个列表并解析列表中的日期属性

for i in listgen:
    #print [i:]        
    dat_e = parser.parse(listgen[0])

预期产出:

2013-07-23 14:08:17.111906+01:00
2013-07-23 14:08:26.630043+01:00
2013-07-23 14:08:27.188360+01:00
2013-07-23 14:08:27.306438+01:00
2013-07-23 14:08:27.342922+01:00
2013-07-23 15:12:10.803993+01:00

3 个答案:

答案 0 :(得分:3)

嗯...

for i in listgen:
    #print [i:]
    print i[0].replace('T', ' ')    

答案 1 :(得分:2)

for item in listgen:
    date = item[0].replace('T', ' ')
    print date
    #do other things with date

答案 2 :(得分:0)

你需要这样做:

假设:

listgen = [['2013-07-23T14:08:17.111906+01:00', 'server24', 'status=inprogress', 'MSG= Transfer Intitated'],['2013-07-23T14:08:26.630043+01:00', 'server x2', 'status=waiting', 'MSG= waiting for response'],['2013-07-23T14:08:27.188360+01:00', 'server35', 'status=waiting', 'MSG= waiting for response'],['2013-07-23T14:08:27.306438+01:00', 'server35x', 'status=successful', 'MSG= Transfer Completed'],['2013-07-23T14:08:27.342922+01:00', 'serverx56', 'status=inprogress', 'MSG= Transfer Intitated'],['2013-07-23T15:12:10.803993+01:00', 'server267', 'status=waiting', 'MSG= waiting for response']]


for i in listgen:
    print i[0] # that's your date part

你可以应用你想要的任何功能(如解析等)。