当我尝试在数据库中插入外来字符时,可能是什么导致此错误?
>>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)
我该如何解决呢?
谢谢!
当我尝试在数据库中插入外来字符时,可能是什么导致此错误?
>>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)
我该如何解决呢?
谢谢!
Answers:
字符U + 201C左双引号不在Latin-1(ISO-8859-1)编码中。
这是目前在代码页1252(西欧)。这是Windows特定的编码,基于ISO-8859-1,但会将额外的字符放入0x80-0x9F范围内。代码页1252通常与ISO-8859-1混淆,这是一种令人讨厌但现在是标准的Web浏览器行为,如果您将页面作为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 Western代码页中的其他字符。但是cp1252中不存在的其他Unicode字符仍然会导致错误。
您可以encode(..., 'ignore')
通过消除字符来抑制错误,但实际上在本世纪,您应该在数据库和页面中都使用UTF-8。此编码允许使用任何字符。理想情况下,您还应该告诉MySQL您正在使用UTF-8字符串(通过在字符串列上设置数据库连接和排序规则),这样它就可以正确区分大小写并进行排序。
cp1252
ISO-8859-1的严格超集吗?即,当浏览器收到ISO-8859-1页面时,他们可以将其呈现为CP1252,因为0x80-0x9F
无论如何该范围中都不会包含任何字符。
使用Python MySQLdb模块时,我遇到了同样的问题。由于MySQL允许您将几乎任何所需的二进制数据存储在文本字段中,而不考虑字符集,因此我在这里找到了解决方案:
编辑:引用以上URL来满足第一个评论中的请求...
“ 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()
。
最好的解决方案是
确实喜欢此评论(添加use_unicode=True
和charset="utf8"
)
db = MySQLdb.connect(host =“ localhost”,用户=“ root”,passwd =“”,db =“ testdb”,use_unicode = True,charset =“ utf8”)– KyungHoon Kim 2014年3月13日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.
"""
您正在尝试\u201c
使用ISO-8859-1 / Latin-1
无法描述该代码点的编码来存储Unicode代码点。您可能需要更改数据库以使用utf-8,并使用适当的编码来存储字符串数据,或者您可能想在存储内容之前清理输入内容;或者 即使用类似Sam Ruby的出色的i18n指南的内容。那将讨论windows-1252
可能引起的问题,并建议如何处理,以及指向示例代码的链接!
Latin-1(aka ISO 8859-1)是一个八位字节的字符编码方案,您不能将\u201c
(“
)放入一个字节中。
您是要使用UTF-8编码吗?
\u0391
适合一个字节(特别是字节193)。您可能想看看它;人们发现它很有帮助。