I get an error message when using strptime
trying to convert a string extracted from a website into a date format. I have to convert the extracted text data into a string which I have done but I get an error message saying:
exceptions.ValueError: time data 'Sat 18th Apr 2015' does not match format '%a %d %b'.
I've checked the documentation and I think I have the right parameters. I really want to save this information into a database in a YYYY,MM,DD
format.
This is how i try to solve the problem
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")
Inserting into a database like so...
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
You don't need re
or dateutil
just rstrip
the letters after splitting:
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