Lesen Wenn ich versuche zu laufen:Django Codierungsfehler, wenn aus einer CSV-
import csv
with open('data.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
pgd = Player.objects.get_or_create(
player_name=row['Player'],
team=row['Team'],
position=row['Position']
)
Die meisten meiner Daten wird in der Datenbank erstellt, mit Ausnahme einer bestimmten Zeile. Wenn mein Skript die Zeile erreicht, erhalte ich die Fehlermeldung:
ProgrammingError: You must not use 8-bit bytestrings unless you use a
text_factory that can interpret 8-bit bytestrings (like text_factory = str).
It is highly recommended that you instead just switch your application to Unicode strings.`
Die bestimmte Zeile in der CSV, die diesen Fehler verursacht, ist:
>>> row
{'FR\xed\x8aD\xed\x8aRIC.ST-DENIS', 'BOS', 'G'}
Ich habe mit dem an den anderen ähnlichen Themen sieht Stackoverflow gleiche oder ähnliche Probleme, aber die meisten sind nicht spezifisch für die Verwendung von Sqlite mit Django. Irgendein Rat?
Wenn es wichtig ist, führe ich das Skript aus, indem ich in die Django-Shell gehe, indem ich python manage.py shell
aufruft, und kopiere es in, anstatt nur das Skript von der Befehlszeile aus aufzurufen.
Dies ist der Stacktrace ich:
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 108, in next
row = self.reader.next()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py", line 302, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 1674: invalid continuation byte
EDIT: habe ich beschlossen, diesen Eintrag in der Datenbank nur manuell zu importieren, anstatt zu versuchen, es aus meiner CSV zu lesen, auf Basis von Alastair McCormack Feedback
Based on the output from your question, it looks like the person who made the CSV mojibaked it - it doesn't seem to represent FRÉDÉRIC.ST-DENIS. You can try using windows-1252 instead of utf-8 but I think you'll end up with FRíŠDíŠRIC.ST-DENIS in your database.
Python 2. x oder 3.x? –
Python 2.x, aber das ist ein neues Projekt. Wenn ich also auf 3.x umsteigen kann, werde ich es einfacher machen. – Konrad