使用strtime(Python)时出现错误消息

使用strtime(Python)时出现错误消息,python,mysql,scrapy,strptime,Python,Mysql,Scrapy,Strptime,当使用strtime尝试将从网站提取的字符串转换为日期格式时,我收到一条错误消息。我必须将提取的文本数据转换为字符串,我已经这样做了,但我收到一条错误消息,上面说: exceptions.ValueError: time data 'Sat 18th Apr 2015' does not match format '%a %d %b'. 我已经检查了文档,我认为我有正确的参数。我真的想以YYYY,MM,DD格式将此信息保存到数据库中 这就是我试图解决问题的方法 item ['date'

当使用
strtime
尝试将从网站提取的字符串转换为日期格式时,我收到一条错误消息。我必须将提取的文本数据转换为字符串,我已经这样做了,但我收到一条错误消息,上面说:

exceptions.ValueError: time data 'Sat 18th Apr 2015' does not match format '%a %d %b'.
我已经检查了文档,我认为我有正确的参数。我真的想以
YYYY,MM,DD
格式将此信息保存到数据库中

这就是我试图解决问题的方法

    item ['date'] = info.xpath('.//span[@class="dates"]//text()').extract() # Extract date information.
    convert = ''.join(str(t)for t in item['date'])+" 2015"
    print convert
    time.strptime(convert, "%a %dth %b %Y")
像这样插入数据库

def process_item(self, item, spider):    
        try:
            self.cursor.execute("INSERT INTO info (venue, datez, dateForm, link, genre) VALUES (%s, %s, %s, %s, %s)", (item['artist'][0], item['date'][0], item['dateForm'][0], item['trackz'], "clubbing"))        
            self.conn.commit()
        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])

            return item

您不需要
re
dateutil
只需要
rstrip
拆分后的字母:

from datetime import datetime

a, b, c = item["date"][0].split()

print(datetime.strptime("{} {} {} {}".format(a,b.rstrip("ndthstr"),c,"2015"),"%a %d %b %Y").strftime("%Y,%m,%d"))

2015,04,18

%d
不支持序数,去掉那里的
th
。年份从哪里来?对不起,忘了把年份放在…关于去掉序数的任何提示?还有,有没有办法让它忽略“Sat”?我已经编辑了这个问题,以显示我是如何获得数据的。。。可能需要另一种方法?谢谢你的休息Padraic@DanielParkin,请尝试
“”。如果t[0],则加入(t.rstrip(“thrdstn”)。isdigit()否则t表示项目['date']中的t)
,它将删除th、nd、st等。。假定每个
t
都是单独的字符串。元素还没有字符串吗?仍然会收到错误消息。这对我来说似乎很奇怪……”exceptions.ValueError:时间数据“2015年4月18日星期六”与格式“%a%d%b%Y”不匹配。能否打印项目[date]并显示其外观?项目日期是在错误消息中打印的,没有“2015”字样。确切地说,“[u'Sat Arth']”是它在终端中的表现