将文本文件内容保存到DB:“不正确的字符串值:'\xEF\xBB\xBF#W ...'for row'contents'in row 1” [英] Saving text file contents to DB: "Incorrect string value: '\xEF\xBB\xBF# W...' for column 'contents' at row 1"

查看:374
本文介绍了将文本文件内容保存到DB:“不正确的字符串值:'\xEF\xBB\xBF#W ...'for row'contents'in row 1”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Django应用程序中,我正在上传一个文本文件,使用file.read()获取文件的内容,然后保存到数据库(使用Django的.save()方法)。

In my Django app, I'm uploading a text file, using file.read() to get the contents of the file, and then saving to the database (using Django's .save() method).

我收到以下错误:

Environment:

Request Method: POST
Request URL: http://localhost:8000/
Django Version: 1.2.5
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.markup',
 'files']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/pymodules/python2.7/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/mcrittenden/Dropbox/Code/dropdo-django/dropdo/files/views.py" in index
  31.                 return handle_upload(request.FILES['file'])
File "/home/mcrittenden/Dropbox/Code/dropdo-django/dropdo/files/views.py" in handle_upload
  60.     file.save()
File "/usr/lib/pymodules/python2.7/django/db/models/base.py" in save
  458.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/pymodules/python2.7/django/db/models/base.py" in save_base
  551.                     result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/pymodules/python2.7/django/db/models/manager.py" in _insert
  195.         return insert_query(self.model, values, **kwargs)
File "/usr/lib/pymodules/python2.7/django/db/models/query.py" in insert_query
  1524.     return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/pymodules/python2.7/django/db/models/sql/compiler.py" in execute_sql
  788.         cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/pymodules/python2.7/django/db/models/sql/compiler.py" in execute_sql
  732.         cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.7/django/db/backends/util.py" in execute
  15.             return self.cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.7/django/db/backends/mysql/base.py" in execute
  86.             return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py" in execute
  168.         if not self._defer_warnings: self._warning_check()
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py" in _warning_check
  82.                     warn(w[-1], self.Warning, 3)

Exception Type: Warning at /
Exception Value: Incorrect string value: '\xEF\xBB\xBF# W...' for column 'contents' at row 1



我假设(因为EF BB BF是UTF BOM字符),这是由于字符集之间的DB和文件?这个声音是否有效?如果是,我该如何解决?

I'm assuming (since EF BB BF is the UTF BOM character) this is due to a difference in charset between the DB and the file? Does that sound valid? If so, how do I fix it?

推荐答案

检查你的数据库的字符集(它是utf-8?)。如果不是,并且您要使用UTF-8,请使用此SQL命令更改字符集

You're on the right path. Check the charset of your database (is it utf-8?). If it isn't and you want to use UTF-8, change the charset by using this SQL command

alter table yourTableName DEFAULT CHARACTER SET utf8;

并阅读这个伟大的教程在Python中使用UTF-8如果你想转换你的UTF-8字符串回来。

And read this great tutorial on using UTF-8 in Python if you want to convert your UTF-8 string back.

您可以使用此命令剥离DOM

You can strip the DOM by using this command


#剥离BOM从Unicode字符串的开头,如果它存在

u.lstrip(unicode(codecs.BOM_UTF8,
utf8))

# Strip the BOM from the beginning of the Unicode string, if it exists
u.lstrip( unicode( codecs.BOM_UTF8, "utf8" ) )

这篇关于将文本文件内容保存到DB:“不正确的字符串值:'\xEF\xBB\xBF#W ...'for row'contents'in row 1”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆