我解析一個csv文件(在windows中創建)並嘗試使用我創建的模型填充數據庫表。Django DB插入不正確的字符串值
我收到此錯誤:
pl = PriceList.objects.create(code=row[0], description=row[1],.........
Incorrect string value: '\xD0h:NAT...' for column 'description' at row 1
我的表和說明字段使用UTF-8和utf8_general_ci排序規則。 我試圖插入的實際值是這樣的。
HOUSING:PS-187:1g\xd0h:NATURAL CO
我不知道有任何字符串處理我應該做,以克服這個錯誤。 我覺得我用一個簡單的Python腳本之前使用conn.escape_string()來填充數據庫和它的工作(如幫助)
感謝
我不想讀取整個文件並把它傳遞給一個函數,因爲該文件包含125K行。另外,'HOUSING:PS-187:1g \ xd0h:NATURAL CO'.encode('utf-8')的結果是:UnicodeDecodeError:'ascii'編解碼器無法解碼位置17中的字節0xd0:序號不在範圍內(128 ) – Dim 2011-05-07 19:00:59
這不會讀取整個文件,它將包裝CSV閱讀器並與csv_reader一起與unicode_csv_reader進行交互。這聽起來像你有一個Python數據編碼問題,而不是Django問題。 – Ted 2011-05-07 19:07:38
有沒有什麼辦法可以消毒輸入,所以我可以解決這個問題? – Dim 2011-05-07 19:23:48