Unicode编码错误:'latin-1'编解码器无法编码字符

121 投票
12 回答
353734 浏览
提问于 2025-04-16 05:32

当我尝试把外文字符插入数据库时,可能是什么原因导致这个错误呢?

>>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)

我该怎么解决这个问题呢?

谢谢!

12 个回答

24

最好的解决办法是:

  1. 把mysql的字符集设置为'utf-8'
  2. 按照这个评论的方式做(添加 use_unicode=Truecharset="utf8"

    db = MySQLdb.connect(host="localhost", user = "root", passwd = "", db = "testdb", use_unicode=True, charset="utf8") – KyungHoon Kim Mar 13 '14 at 17:04

详细内容请见:

class Connection(_mysql.connection):

    """MySQL Database Connection Object"""

    default_cursor = cursors.Cursor

    def __init__(self, *args, **kwargs):
        """

        Create a connection to the database. It is strongly recommended
        that you only use keyword parameters. Consult the MySQL C API
        documentation for more information.

        host
          string, host to connect

        user
          string, user to connect as

        passwd
          string, password to use

        db
          string, database to use

        port
          integer, TCP/IP port to connect to

        unix_socket
          string, location of unix_socket to use

        conv
          conversion dictionary, see MySQLdb.converters

        connect_timeout
          number of seconds to wait before the connection attempt
          fails.

        compress
          if set, compression is enabled

        named_pipe
          if set, a named pipe is used to connect (Windows only)

        init_command
          command which is run once the connection is created

        read_default_file
          file from which default client values are read

        read_default_group
          configuration group to use from the default file

        cursorclass
          class object, used to create cursors (keyword only)

        use_unicode
          If True, text-like columns are returned as unicode objects
          using the connection's character set.  Otherwise, text-like
          columns are returned as strings.  columns are returned as
          normal strings. Unicode objects will always be encoded to
          the connection's character set regardless of this setting.

        charset
          If supplied, the connection character set will be changed
          to this character set (MySQL-4.1 and newer). This implies
          use_unicode=True.

        sql_mode
          If supplied, the session SQL mode will be changed to this
          setting (MySQL-4.1 and newer). For more details and legal
          values, see the MySQL documentation.

        client_flag
          integer, flags to use or 0
          (see MySQL docs or constants/CLIENTS.py)

        ssl
          dictionary or mapping, contains SSL connection parameters;
          see the MySQL documentation for more details
          (mysql_ssl_set()).  If this is set, and the client does not
          support SSL, NotSupportedError will be raised.

        local_infile
          integer, non-zero enables LOAD LOCAL INFILE; zero disables

        autocommit
          If False (default), autocommit is disabled.
          If True, autocommit is enabled.
          If None, autocommit isn't set and server default is used.

        There are a number of undocumented, non-standard methods. See the
        documentation for the MySQL C API for some hints on what they do.

        """
101

我在使用Python的MySQLdb模块时也遇到了同样的问题。因为MySQL允许你在文本字段中存储几乎任何二进制数据,而不管字符集是什么,所以我在这里找到了我的解决方案:

在Python MySQLdb中使用UTF8

编辑:引用上面网址的内容,以满足第一个评论中的请求...

"UnicodeEncodeError:'latin-1' 编码无法编码字符 ..."

这是因为MySQLdb通常会尝试将所有内容编码为latin-1。这个问题可以通过在建立连接后立即执行以下命令来解决:

db.set_character_set('utf8')
dbc.execute('SET NAMES utf8;')
dbc.execute('SET CHARACTER SET utf8;')
dbc.execute('SET character_set_connection=utf8;')

"db"是MySQLdb.connect()的结果,而"dbc"是db.cursor()的结果。

82

字符 U+201C 左双引号在 Latin-1(ISO-8859-1)编码中是不存在的。

但它在代码页 1252(西欧)中是有的。这个编码是专门为 Windows 系统设计的,基于 ISO-8859-1,但在 0x80-0x9F 的范围内添加了一些额外的字符。代码页 1252 常常和 ISO-8859-1 混淆,而这也是一个让人烦恼但现在已经成为标准的网页浏览器行为:如果你把网页设置为 ISO-8859-1,浏览器会把它当作 cp1252 来处理。不过,它们实际上是两种不同的编码:

>>> u'He said \u201CHello\u201D'.encode('iso-8859-1')
UnicodeEncodeError
>>> u'He said \u201CHello\u201D'.encode('cp1252')
'He said \x93Hello\x94'

如果你只是把数据库当作字节存储,可以使用 cp1252 来编码 和其他在 Windows 西欧代码页中存在的字符。但如果有其他 Unicode 字符在 cp1252 中不存在,就会出现错误。

你可以使用 encode(..., 'ignore') 来抑制这些错误,方法是去掉那些字符。但实际上,在这个世纪,你应该在数据库和网页中都使用 UTF-8 编码。这个编码允许使用任何字符。你还应该告诉 MySQL 你在使用 UTF-8 字符串(通过设置数据库连接和字符串列的排序规则),这样它才能正确进行不区分大小写的比较和排序。

撰写回答