StackOverflow上的类似问题似乎与我没有相同的问题,我只找到http://qnundrum.com/question/766895,但从未回答过。我正在运行Python 3.3和Django 1.6,所以通常会自动处理Unicode内容。感谢我能得到的任何帮助。
我正在尝试将自动生成的电子书保存到我的数据库中以供日后检索。有些书籍包含非ASCII字符。我已经完成了这一代,.mobi和.epub的工作。这是models.py:
class StoryDownload(models.Model):
text = models.OneToOneField('stories.Story', primary_key=True, related_name='downloads')
epub = models.FileField(upload_to='epub/', blank=True, null=True)
mobi = models.FileField(upload_to='mobi/', blank=True, null=True)
def update_downloads(self):
#code to generate epub and mobi files from text
...
self.epub = File(open('filename.epub'), 'r'))
self.mobi = File(open('filename.mobi'), 'r'))
self.save()
...
self.save()
出现错误让我感到困惑;如果文件被接受为Django File对象,为什么我不能保存它们?
回溯:
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\braces\views\_access.py" in dispatch
64. request, *args, **kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\guardian\mixins.py" in dispatch
190. **kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\views\generic\base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\views\generic\edit.py" in post
228. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\views\generic\edit.py" in post
171. return self.form_valid(form)
File "C:\Users\Chris\Envs\stories\dev\akrito\chapters\views.py" in form_valid
69. self.chapter.story.save()
File "C:\Users\Chris\Envs\stories\dev\akrito\stories\models.py" in save
87. self.downloads.update_downloads()
File "C:\Users\Chris\Envs\stories\dev\akrito\stories\models.py" in update_downloads
135. self.save()
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\base.py" in save_base
573. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\base.py" in _save_table
632. for f in non_pks]
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\base.py" in <listcomp>
632. for f in non_pks]
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\fields\files.py" in pre_save
252. file.save(file.name, file, save=False)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\db\models\fields\files.py" in save
86. self.name = self.storage.save(name, content)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\core\files\storage.py" in save
49. name = self._save(name, content)
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\core\files\storage.py" in _save
203. for chunk in content.chunks():
File "C:\Users\Chris\Envs\stories\lib\site-packages\django\core\files\base.py" in chunks
76. data = self.read(chunk_size)
File "C:\Users\Chris\Envs\stories\lib\encodings\cp1252.py" in decode
23. return codecs.charmap_decode(input,self.errors,decoding_table)[0]
Exception Type: UnicodeDecodeError at /stories/2/1/e/
Exception Value: 'charmap' codec can't decode byte 0x81 in position 123: character maps to <undefined>
答案 0 :(得分:3)
对于文本文件,您需要使用所需的open
来呼叫encoding
。默认编码为locale.getpreferredencoding(False)
,这就是回溯显示它尝试使用Windows 1252代码页进行解码的原因。
也就是说,MOBI和EPUB文件是压缩档案,应该以二进制模式打开,例如open('filename.epub', 'rb')
。