with open('H:/Controlling/Py/DB_Mitarbeiter.csv', mode='r') as ifile:
File "<ipython-input-69-95d3a75e0e49>", line 1
with open('H:/Controlling/Py/DB_Mitarbeiter.csv', mode='r') as ifile:
@sandordan: Die Antwort bleibt die gleiche, in dem ``with``-Block muss etwas stehen, und wenn es nur ein ``pass`` ist oder ``...`` (Syntax für ein `Ellipsis`-Objekt).
Bei CSV-Dateien die mit dem `csv`-Modul vearbeitet werden sollen muss man noch ein ``newlinw=""`` beim öffnen übergeben und man sollte bei Textdateien immer explizit die Kodierung angeben.
Namen sollten keine kryptischen Abkürzungen enthalten. Wenn man `input_file` meint, sollte man nicht nur `ifile` schreiben.
Please call it what it is: copyright infringement, not piracy. Piracy takes place in international waters, and involves one or more of theft, murder, rape and kidnapping. Making an unauthorized copy of a piece of software is not piracy, it is an infringement of a government-granted monopoly.
Blöcke werden in Python durch Einrückungen gekennzeichnet.
Das with-Statement sorgt dafür, dass etwas in nachfolgenden Block zur Verfügung steht und nach Verlassen des Blocks korrekt geschlossen wird.
Dafür muss nach dem with aber auch ein Block folgen.
Bei dir ist da aber nichts eingerückt.
Benutze keine Abkürzungen: conn -> connection
Dateien für csv müssen mit newline="" geöffnet werden.
Das SQL-Statement ist nicht gültig. mydb wird nirgends definiert.
import mysql.connector
import csv
connection = mysql.connector.connect(user='X',password='Y', database='km_db')
cursor = connection.cursor()
with open('test.csv', encoding='utf8', newline='') as input_file:
rows = csv.reader(input_file, delimiter=';')
for row in rows:
cursor.execute("INSERT INTO test_tbl (name_txt, ID_1) VALUES (%s, %s)", row)
connection.commit()
cursor.close()
print("Done")
Die Tabelle und Felder sind allesamt schlecht benannt. Das tbl hat im Tabellennamen nichts zu suchen. Abkürzungen erst recht nichts. Dass es sich um einer Tabelle handelt, wird schon aus der Position der Verwendung klar. Was soll das txt bei name_txt? ID_1? Um welche ID handelt es sich denn?
Das wäre vielleicht auch ein Fall für `executemany()`.
Please call it what it is: copyright infringement, not piracy. Piracy takes place in international waters, and involves one or more of theft, murder, rape and kidnapping. Making an unauthorized copy of a piece of software is not piracy, it is an infringement of a government-granted monopoly.
with open('test.csv', encoding='utf8', newline='') as input_file:
File "<ipython-input-11-ecec768d3349>", line 1
with open('test.csv', encoding='utf8', newline='') as input_file:
^
SyntaxError: unexpected EOF while parsing
@sandordan: Der muss halt schon vollständig sein. In Sirius3's Beispiel folgen auf diese Zeile ja noch sieben weitere Zeilen. Die müssen da halt auch stehen wenn Du das an dem Python-Compiler verfüttern willst.
Please call it what it is: copyright infringement, not piracy. Piracy takes place in international waters, and involves one or more of theft, murder, rape and kidnapping. Making an unauthorized copy of a piece of software is not piracy, it is an infringement of a government-granted monopoly.