ich komme bei meinem Programm nicht weiter. Ich versuche eine PayPal CSV Datei in eine SQLite Datenbank zu kopieren. Hänge aber an einem KeyError den ich auf ein Problem mit dem Encoding zurückführe. Ich dachte eigentlich das habe ich schon soweit im Griff, kann jedoch die CSV File nicht mal codecs.open öffnen. Das strip() wird verwendet da bei der PayPal CSV ein Leerzeichen vor dem Titel der Spalte steht.
Die CSV Datei stelle ich hier zum herunterladen bereit. Encoding sollte eigentlich iso-8859-15 bzw latin-1 sein.
CSV File: http://www.file-upload.net/download-860 ... n.csv.html
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf8 -*-
import csv
import codecs
import time
from sqlite3 import dbapi2 as sqlite
#.decode('latin')
#.encode('utf8')
def readPayPalCSV():
with codecs.open('./Data/Herunterladen.csv', 'rb') as csvfile:
csvReader = csv.DictReader(csvfile, delimiter=",")
header = [c for c in csvReader.fieldnames if c.strip()]
print "\n header", header
csvcontent = []
for c in csvReader:
newdict = {}
for h in header:
newdict[h.strip()] = c[h]
csvcontent.append(newdict)
print "\n PayPal Liste\n%s Einträge" % len(csvcontent)
return csvcontent
paypal_dict_list = readPayPalCSV()
#connect to db
con = sqlite.connect("Data/database_test")
cur = con.cursor()
#del db and create new db
def create_db():
#paypal
cur.execute('''drop table if exists paypal_complete''')
cur.execute('''create table if not exists paypal_complete (Datum date, Zeit time, Zeitzone text, Name text, Art text, Status text, Währung text, Brutto text, Gebühr text, Netto text, [Von E-Mail-Adresse] text, [An E-Mail-Adresse] text, Transaktionscode text PRIMARY KEY, [Status der Gegenpartei] text, Adressstatus text, Verwendungszweck text, Artikelnummer text, [Betrag für Versandkosten] text, Versicherungsbetrag text, Umsatzsteuer text, [Option 1 - Name] text, [Option 1 - Wert] text, [Option 2 - Name] text, [Option 2 - Wert] text, [Auktions-Site] text, [Käufer-ID] text, [Artikel-URL] text, Angebotsende date, [Vorgangs-Nr.] text, [Rechnungs-Nr.] text, [Txn-Referenzkennung] text, Rechnungsnummer text, [Individuelle Nummer] text, Bestätigungsnummer text, Guthaben text, Adresse text, [Zusätzliche Angaben] text, Ort [text], [Staat/Provinz/Region/Landkreis/Territorium/Präfektur/Republik] text, PLZ text, Land text, [Telefonnummer der Kontaktperson] text)''')
create_db()
def print_header(tabelle):
print "\n header for", tabelle
cur.execute("PRAGMA table_info("+tabelle+")")
print cur.fetchall()
print_header("paypal_complete")
def insertDict(curs, tablename, data_list):
for data in data_list:
fields = ['[' + k + ']' for k in data.keys()]
values = ['[' + v + ']' for v in data.values()]
placeholder = "?"
fieldlist = ",".join(fields)
placeholderlist = ",".join([placeholder] * len(fields))
query = "insert into %s(%s) values (%s)" % (tablename, fieldlist, placeholderlist)
print "query"
print query
curs.execute(query, values)
insertDict(cur, "paypal_complete", paypal_dict_list)
#show first db entries
def print_lines(db, count=5):
print "\n", db
cur.execute('select * from '+db)
for n, a in enumerate(cur):
if n == count:
break
else:
print a
print_lines("paypal_complete")
#close connection
con.commit()