Python时间戳正则表达式

scr*_*ddy 2 python expression timestamp

有人帮我处理我的代码,我将数据从csv文件写入timeStamp列表吗?列表中的数据目前的格式如此03.08.2012 07.11.15 PM.我需要将07:11:15 PM的时间放入actTime阵列.这是我的代码:

import csv
import re
reader = csv.reader(open('main.csv','rb'), delimiter=',',quotechar="'")
timeStamp = []
ask = []
regexp = re.compile('\d{2}:\d{2}:\d{4}')
actTime = []
x = 0
try:
    for row in reader:
        ask.append(row[5:6])
        timeStamp.append(row[7:8])
except csv.Error, e:
    sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
for item in timeStamp:
    actTime.append(timeStamp[x])
    match = regexp.match(timeStamp[x])
    if match:
        time = int(match.group[x])
    x = x + 1
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误消息:

回溯(最近一次调用最后一次):文件"rates.py",第17行,在match = regexp.match(timeStamp [x])TypeError:期望的字符串或缓冲区

Kat*_*iel 6

请改用内置时间戳解析机制.

>>> import datetime
>>> t = "03.08.2012 07.11.15 PM"
>>> u = datetime.datetime.strptime(t, "%d.%m.%Y %I.%M.%S %p")
>>> u
datetime.datetime(2012, 8, 3, 19, 11, 15)
>>> u.strftime("%I:%M:%S %p")
'07:11:15 PM'
Run Code Online (Sandbox Code Playgroud)