ich ein Lernprojekt auf Udemy folgen
namens "30 Days of Python | Unlock your Python Potential" Und das ist die courese von Day16python3 CSV Typeerror Valueerror _csv.Error
Hier ist mein Code
filename = "data.csv"
temp_file = NamedTemporaryFile(delete=False)
with open(filename, "rb") as csvfile, temp_file:
reader = csv.DictReader(csvfile)
fieldnames = ["id", "name", "email", "amount", "sent"]
writer = csv.DictWriter(temp_file, fieldnames=fieldnames)
#writer.writeheader()
for row in reader:
print(row)
writer.writerow({
"id": row["id"],
"name": row["name"],
"email": row["email"],
"amount": "1293.23",
"sent": ""
})
bekomme ich einen Fehler, obwohl ich, dass mein Code überprüft ist die gleiche wie Lehrer Code
Traceback (most recent call last):
File "hungry_data.py", line 36, in <module>
for row in reader:
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 109, in __next__
self.fieldnames
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 96, in fieldnames
self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
die refore änderte ich Code von "rb" auf "r" .... with open(filename, "r") as csvfile, temp_file:
und auf diese Weise zu
with open(filename, "r", newline='') as csvfile, temp_file:
Aber die gleichen Fehler
Traceback (most recent call last):
File "hungry_data.py", line 43, in <module>
"sent": ""
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 153, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
Es scheint, dass die Linie 36 habe einen Konflikt mit Zeile 43 wie soll ich tun ...