讀書時當我嘗試運行:Django的編碼錯誤從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']
)
我的大部分數據的獲取在數據庫中創建,除了一個特定的行。當我的腳本達到此行,我收到錯誤:
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.`
在CSV特定行導致此錯誤是:
>>> row
{'FR\xed\x8aD\xed\x8aRIC.ST-DENIS', 'BOS', 'G'}
我已經看過了其他類似的線程#1與相同或相似的問題,但大多數並不特定於在Django中使用Sqlite。有什麼建議?
如果很重要,我通過調用python manage.py shell
進入Django shell並複製粘貼它來運行腳本,而不是從命令行調用腳本。
這是堆棧跟蹤我得到:
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
編輯:我決定與這個詞條只是手動導入到我的數據庫,而不是試圖從我的CSV,根據阿拉斯泰爾·麥科馬克的反饋
閱讀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或3.x? –
Python 2.x,但這是一個新項目,所以如果切換到3.x將使我的生活更輕鬆,我會這樣做。 – Konrad