【问题标题】:UnicodeDecodeError on Pycharm ShellPycharm Shell 上的 UnicodeDecodeError
【发布时间】:2015-10-13 19:37:38
【问题描述】:

我正在使用 Pycharm 和 Python 3.4 开发 Django 1.8.4 项目

我试图在 shell 上创建一个模型对象来测试一些东西,并且在执行这一行时:

song1 = Cancion.objects.create(nombre="City Lights",
                               audio_file=File(open("C:\\Users\\pablo\\Desktop\\Music\\Brian Culbertson  - Another Long Night Out (2014)\\01 - City Lights (Feat. Lee Ritenour).mp3",encoding="utf-8")))

我收到此错误:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 348, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 762, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 846, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 885, in _do_insert
    using=using, raw=raw)
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 973, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 931, in as_sql
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 931, in <listcomp>
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 929, in <listcomp>
    ) for f in fields
  File "C:\Python34\lib\site-packages\django\db\models\fields\files.py", line 315, in pre_save
    file.save(file.name, file, save=False)
  File "C:\Python34\lib\site-packages\django\db\models\fields\files.py", line 94, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "C:\Python34\lib\site-packages\django\core\files\storage.py", line 64, in save
    name = self._save(name, content)
  File "C:\Python34\lib\site-packages\django\core\files\storage.py", line 253, in _save
    for chunk in content.chunks():
  File "C:\Python34\lib\site-packages\django\core\files\base.py", line 85, in chunks
    data = self.read(chunk_size)
  File "C:\Python34\lib\codecs.py", line 313, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 21: invalid start byte

谁能告诉我我的命令的编码问题在哪里?

谢谢!

【问题讨论】:

  • 您是否尝试过在不使用encoding='utf-8' 的情况下使用open()?编码只能用于文本。
  • 是的,我尝试删除编码并收到此错误UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 586: character maps to &lt;undefined&gt;
  • 默认模式是'r'(读取文本文件),使用open("C:...", mode='b')
  • 对不起,必须是mode='rb'
  • 谢谢!它现在解决了他的问题 :) 您能否将其发布为答案,以便我检查它是否已解决?

标签: python django shell


【解决方案1】:

使用open 这样的open('C:\\...', encoding='utf-8') 函数意味着,使用utf-8 编码读取我的文本文件。由于您尝试读取 mp3 文件(二进制),因此这是行不通的。

读取方式是:open('C:\\coolSong.mp3', mode='rb'),意思是读取我的二进制文件。

在这里结帐关于the open function and it's modesoverview about io 的非常好的解释。

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2013-09-27
    相关资源
    最近更新 更多