Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeEncodeError:&x27;拉丁语-1';编解码器可以';t编码字符_Python_Mysql_Unicode_Pylons - Fatal编程技术网

Python UnicodeEncodeError:&x27;拉丁语-1';编解码器可以';t编码字符

Python UnicodeEncodeError:&x27;拉丁语-1';编解码器可以';t编码字符,python,mysql,unicode,pylons,Python,Mysql,Unicode,Pylons,当我尝试将外来字符插入数据库时,是什么导致了此错误 >>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256) 我该如何解决它 谢谢 您试图使用无法描述该码点的编码ISO-8859-1/拉丁语-1存储Unicode码点\u201c。您可能需要更改数据库以使用utf-8,并使用适当的编码存储字符串数据,或者您可能希望在存储

当我尝试将外来字符插入数据库时,是什么导致了此错误

>>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)
我该如何解决它


谢谢

您试图使用无法描述该码点的编码
ISO-8859-1/拉丁语-1
存储Unicode码点
\u201c
。您可能需要更改数据库以使用utf-8,并使用适当的编码存储字符串数据,或者您可能希望在存储内容之前对输入进行清理;i、 e.使用。这篇文章讨论了
windows-1252
可能导致的问题,并建议了如何处理这些问题,以及指向示例代码的链接

拉丁语-1(aka)是一种单八位字符编码方案,不能将
\u201c
)放入一个字节


你是想使用UTF-8编码吗?

我希望你的数据库至少是UTF-8。然后你需要运行
yourstring.encode('UTF-8')
,然后再尝试将其放入数据库。

字符U+201C左双引号在拉丁-1(ISO-8859-1)编码中不存在

代码页1252(西欧)。这是一种基于ISO-8859-1的Windows特定编码,但会在0x80-0x9F范围内添加额外字符。代码页1252经常与ISO-8859-1混淆,如果您将页面作为ISO-8859-1提供,浏览器会将其视为cp1252,这是一种恼人但现在是标准的web浏览器行为。然而,它们确实是两种不同的编码:

>>> u'He said \u201CHello\u201D'.encode('iso-8859-1')
UnicodeEncodeError
>>> u'He said \u201CHello\u201D'.encode('cp1252')
'He said \x93Hello\x94'
如果仅将数据库用作字节存储,则可以使用cp1252对Windows西部代码页中的
和其他字符进行编码。但cp1252中不存在的其他Unicode字符将导致错误


您可以使用
encode(…,'ignore')
通过去除字符来抑制错误,但实际上在本世纪,您应该在数据库和页面中都使用UTF-8。这种编码允许使用任何字符。您最好告诉MySQL您使用的是UTF-8字符串(通过设置数据库连接和字符串列的排序规则),这样就可以正确地进行不区分大小写的比较和排序。

我在使用Python MySQLdb模块时遇到了同样的问题。由于MySQL允许您在文本字段中存储所需的任何二进制数据,而不考虑字符集,因此我在这里找到了解决方案:

编辑:引用上述URL以满足第一条评论中的请求

“UnicodeEncodeError:'latin-1'编解码器无法对字符进行编码…”

这是因为MySQLdb通常尝试将所有内容编码为拉丁语-1。 这可以通过在之后立即执行以下命令来解决 您已删除连接:

“db”是MySQLdb.connect()的结果,而“dbc”是
db.cursor()


Python:您需要添加 #-*-编码:UTF-8-*-(删除*周围的空格) 到python文件的第一行。然后将以下内容添加到要编码的文本中:.encode('ascii','xmlcharrefreplace')。这将用ascii等效字符替换所有unicode字符。

最佳解决方案是

  • 将mysql的字符集设置为“utf-8”
  • 喜欢此评论(添加
    使用\u unicode=True
    charset=“utf8”

    db=MySQLdb.connect(host=“localhost”,user=“root”,passwd=“”,db=“testdb”,使用unicode=True,charset=“utf8”)–KyungHoon Kim Mar 13点14分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.
    
            """
    

    SQLAlchemy用户只需将其字段指定为
    convert\u unicode=True

    例如:
    sqlalchemy.String(1000,convert\u unicode=True)

    SQLAlchemy只接受unicode对象并返回它们,处理编码本身


    使用下面的代码片段将文本从拉丁语转换为英语

    import unicodedata
    def strip_accents(text):
        return "".join(char for char in
                       unicodedata.normalize('NFKD', text)
                       if unicodedata.category(char) != 'Mn')
    
    strip_accents('áéíñóúü')
    
    输出:

    “埃伊诺”


    cp1252
    不是ISO-8859-1的严格超集吗?也就是说,当浏览器接收到ISO-8859-1页面时,他们可以将其呈现为cp1252,因为无论如何都不会有范围
    0x80-0x9F
    中的任何字符。不,字节0x80-0x9F在ISO-8859-1中有实际的赋值,这些赋值会被cp1252的添加内容覆盖,因此不会发生错误一个超集。它们精确地映射到Unicode字符U+0080–U+009F,这是一个控制字符的选择。它们是很少使用的控制字符,这就是为什么浏览器没有使用它,但当您试图将字节序列转换为Unicode时,这很烦人。这是我唯一一次看到这个范围内的字符编码为ISO-8859-1或UTF-8的文件中的U+0080-U+009F是由一些小丑连接了一堆文件(其中一些文件是用cp850编码的),然后将结果从“latin1”转换为UTF-8。HTML5规范草案正在考虑认可这种非常实用的浏览器行为(以及一大堆类似的情况)--请参见拉丁语-1编码特定的Unicode字符,而不是那个字符。如果\u201c不能放入一个字节,这并不重要。windows-1252也是一个单八位编码方案,并且包含了\u201c。cp1253(又名windows-1253)也是一个单八位字符编码方案,但是
    \u0391
    可以很好地放入一个字节(特别是字节193)。您可能想看一看;人们发现它很有用。Unicode将拉丁语-1/cp1253符号作为16位代码点合并在一起。我很惊讶这些评论似乎声称相反。建议在答案中提供链接项的相关部分。额外阅读的链接很好,但请尝试插入exe可以这么说,您的答案中有一个可爱的摘要:)非常感谢,在尝试了1000件其他事情后,效果非常好。只有db.set\u character\u set('utf8')才能解决问题db=MySQLdb.connect(host=“localhost”,user=“root”,passwd=“”
    import unicodedata
    def strip_accents(text):
        return "".join(char for char in
                       unicodedata.normalize('NFKD', text)
                       if unicodedata.category(char) != 'Mn')
    
    strip_accents('áéíñóúü')