使用 strptime 时的错误消息 (Python)

Error message when using strptime (Python)

我在使用 strptime 尝试将从网站提取的字符串转换为日期格式时收到错误消息。我必须将提取的文本数据转换为已完成的字符串,但我收到一条错误消息:

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

你不需要 redateutil 只需要 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