2011-05-07 61 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