2011-05-07 28 views
2

我解析一個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()來填充數據庫和它的工作(如幫助)

感謝

回答

0

我像以前一樣遇到了麻煩的CSV讀者和unicode好。在我的情況下使用以下讓我過去的錯誤。

http://docs.python.org/library/csv.html

The csv module doesn’t directly support reading and writing Unicode, ...

unicode_csv_reader() below is a generator that wraps csv.reader to handle Unicode CSV data (a list of Unicode strings). utf_8_encoder() is a generator that encodes the Unicode strings as UTF-8, one string (or row) at a time. The encoded strings are parsed by the CSV reader, and unicode_csv_reader() decodes the UTF-8-encoded cells back into Unicode:

import csv 

def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): 
    # csv.py doesn't do Unicode; encode temporarily as UTF-8: 
    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), 
          dialect=dialect, **kwargs) 
    for row in csv_reader: 
     # decode UTF-8 back to Unicode, cell by cell: 
     yield [unicode(cell, 'utf-8') for cell in row] 

def utf_8_encoder(unicode_csv_data): 
    for line in unicode_csv_data: 
     yield line.encode('utf-8') 
+0

我不想讀取整個文件並把它傳遞給一個函數,因爲該文件包含125K行。另外,'HOUSING:PS-187:1g \ xd0h:NATURAL CO'.encode('utf-8')的結果是:UnicodeDecodeError:'ascii'編解碼器無法解碼位置17中的字節0xd0:序號不在範圍內(128 ) – Dim 2011-05-07 19:00:59

+0

這不會讀取整個文件,它將包裝CSV閱讀器並與csv_reader一起與unicode_csv_reader進行交互。這聽起來像你有一個Python數據編碼問題,而不是Django問題。 – Ted 2011-05-07 19:07:38

+0

有沒有什麼辦法可以消毒輸入,所以我可以解決這個問題? – Dim 2011-05-07 19:23:48