打开excel文件时Django csv UnicodeDecodeError

时间:2016-08-22 11:49:10

标签: django csv

我有一个模型:(Python 3.5)

class Model(models.Model):
  import_file = models.FileField(upload_to="import")

  def save(self, *args, **kwargs):
    super(Model, self).save(*args, **kwargs)
    with open(self.import_file.name) as csvfile:
      reader = DictReader(csvfile, dialect='excel')
      for line in reader:
         print (line)

我得到了

'utf-8' codec can't decode byte 0x95 in position 15: invalid start byte
The string that could not be encoded/decoded was: !f�}

我尝试使用utf16,但随后我收到了BOM错误。

你们有什么想法吗?

EDIT1 我刚试过

...
with open(self.import_file.name, encoding='utf-8') as csvfile:     
...

仍然是同样的错误:

'utf-8' codec can't decode byte 0x95 in position 15: invalid start byte

EDIT2

如果我将编码改为ISO-8859-1,我得到了:

line contains NULL byte

0 个答案:

没有答案