[英]Django-Adaptors TypeError: “delimiter” must be an 1-character string
I am getting an error when trying to import some data into my model. 尝试将一些数据导入模型时出现错误。 The error I'm getting is TypeError: complaining about the delimiter I'm using.
我得到的错误是TypeError:抱怨我正在使用的分隔符。
Below is my model for the CSV import, I am using the default delimiter suggested by the documentation. 以下是我用于CSV导入的模型,我使用的是文档建议的默认分隔符。
class SkuCsvModel(CsvModel):
sku_num = models.CharField()
sku_category = models.ForeignKey(SkuCategory)
short_desc = models.CharField()
class Meta:
delimiter = ";"
dbModel = Sku
The CSV file I'm trying to use is below: 我尝试使用的CSV文件如下:
1365400;9;3/8 BALL VALVE
1401901;9;BRASS ELBOW
1406300;9;HOSE BARB, NPT
The code I'm testing in the manage.py shell is: 我正在manage.py shell中测试的代码是:
>>> from core.models import SkuCsvModel
>>> my_csv_list = SkuCsvModel.import_data(data = open("labconco.csv"))
And finally the error I'm getting is: 最后我得到的错误是:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\bin\Python27\lib\site-packages\adaptor\model.py", line 197, in import_data
return importer.import_data(data)
File "E:\bin\Python27\lib\site-packages\adaptor\model.py", line 466, in import_data
for line in csv.reader(data, delimiter=self.delimiter):
TypeError: "delimiter" must be an 1-character string
So I've been fiddling around with the django-adaptor tools, and this error is coming from the import_data() method of the CsvImporter, when I try and put a delimiter directly into the csv.reader(data, delimiter=';') this works fine and I'm able to see the file correctly. 因此,我一直在摆弄django-adaptor工具,当我尝试将定界符直接放入csv.reader(data,delimiter =';;')时,此错误来自CsvImporter的import_data()方法。 ),这样可以正常工作,并且我可以正确看到该文件。 But no matter how I try and enter this import_data method sending in a ';'
但是无论我如何尝试输入此import_data方法,并以';'形式发送 will generate an error.
会产生一个错误。
Look at the snippet below. 查看下面的代码段。 If I provide an integer as a delimiter, it fails with the same exception as in your example.
如果我提供一个整数作为定界符,则它会失败,但异常与您的示例相同。 If I provide a semicolon as a delimiter to csv.reader it works.
如果我提供分号作为csv.reader的分隔符,则可以使用。 This is basically what is done in model.CsvImporter.import_data() as you already found out.
正如您已经发现的,这基本上是在model.CsvImporter.import_data()中完成的。
>>> import csv
>>> import StringIO
>>> io = StringIO.StringIO('name;surname\nsascha;gottfried')
>>> for line in csv.reader(io, delimiter=10):
... print line
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: "delimiter" must be an 1-character string
>>> for line in csv.reader(io, delimiter=';'):
... print line
...
['name', 'surname']
['sascha', 'gottfried']
To debug the situation dump the current value of 'self.delimiter' to the console or similar before it will be passed as a delimiter to csv.reader(). 为了调试情况,将“ self.delimiter”的当前值转储到控制台或类似的设备,然后将其作为定界符传递给csv.reader()。 It must be a different value and/or type than ';'.
它的值和/或类型必须与“;”不同。 Looking at the code of django-adaptors you can validate your django-adaptors model definition with this base class method as well to debug.
查看django-adaptors的代码,您可以使用此基类方法来验证django-adaptors模型定义,并进行调试。 This call should print out what you defined as Meta.delimiter.
该调用应打印出您定义为Meta.delimiter的内容。
>>> from core.models import SkuCsvModel
>>> SkuCsvModel.has_class_delimiter()
But it is pretty fine to omit a delimiter definition and call 'import_from_file' on the model. 但是,省略定界符定义并在模型上调用'import_from_file'很好。 Make sure there is no class delimiter defined.
确保没有定义类分隔符。 If so the importer runs a CSV sniff to detect the delimiter from the file you passed.
如果是这样,导入程序将运行CSV嗅探以从传递的文件中检测定界符。 If you provide the file you mentioned, the sniffer will detect a ';'
如果提供您提到的文件,则嗅探器将检测到“;” and sets self.delimiter.
并设置self.delimiter。
>>> from core.models import SkuCsvModel
>>> SkuCsvModel.has_class_delimiter()
None
>>> my_csv_list = SkuCsvModel.import_from_file(file = open("labconco.csv"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.