ValueError: time data 'Date' does not match format 'Time'
Ja, das ist richtig. weil das Format in der CSV Datei 01.03.2016, 19:30:00, (Monat, Tag, Jahr) ist. Die richtige Syntax von Time aber datetime.strptime('2012-11-14 14:32:30', '%Y-%m-%d %H:%M:%S') ist. Ich möchte das Datum und Zeitformat auf keinen Fall verändern.
Bitte schaut Euch einmal den Code an. Ist das soweit richtig? Ist der \ für den Umbruch richtig, dmit die Zeilen nicht zu lang werden?
Code: Alles auswählen
#!/usr/bin/python
# -*- coding:utf-8 -*-
import csv
import datetime
import glob
import MySQLdb
import os
# Traverse the directory and process each .csv file
for csvfile in glob.glob('data/*.csv'):
# Remove the path and extension and use what's as a table name
table_name = os.path.splitext(os.path.basename(csvfile))[0]
print (table_name)
# Read data from the csv File(s)
with open(csvfile, "rb") as sourcefile:
print(' ')
reader = csv.DictReader(sourcefile, delimiter = ',')
for row in reader:
print("{Date}, {Time}, {Open}, {High}, {Low}, {Close}".format(**row))
Datetime = datetime.datetime.strptime("Date","Time")
try:
db = MySQLdb.connect(host="localhost", user="usr", passwd="passwd", db="db")
cursor = db.cursor()
sql = """INSERT INTO CURRENCY(AUDCAD, AUDJPY, AUDNZD, AUDUSD, CADJPY, EURAUD, EURCAD, EURCHF, \
EURGBP, EURJPY, EURNZD, EURUSD, GBPAUD, GBPJPY, GBPUSD, NZDJPY, \
USDCAD, USDCHF, USDJPY, Datetime, Open, High, Low, Close)
Values("%s", "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s", "%s","%s", "%s", "%s", \
"%s", "%s","%s", "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s")"""
cursor.execute(sql)
db.commit()
db.rollback()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
finally:
if db:
db.close()