Python unicode 编码问题

Python unicode encoding issue(Python unicode 编码问题)
本文介绍了Python unicode 编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用 python 2.7.5.所有数据库和表都是

Using python 2.7.5. All databases and tables are

我的代码看起来像这样:

My code looks like that:

import MySQLdb as mdb
import urllib2
import sys
import logging
logging.basicConfig(level=logging.INFO)

from bs4 import BeautifulSoup as BS
con = mdb.connect('loclhost', 'root', '', 'mydb');
cur = con.cursor()
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
with con:
...
        sql_insert = """INSERT INTO Teams (name, category, countryId) VALUES (%s, 1, %s)"""
        cursor = con.cursor()
        try:
            affected_count = cursor.execute(sql_insert, (name, id))  <<< this line
            con.commit()
        except mdb.IntegrityError:
            logging.warn("failed to insert values %s, %s", name, id)
        finally:
           cursor.close()
...

con.close()

获取错误信息:

"UnicodeEncodeError: 'latin-1' codec can't encode character u'\u015f'在位置 2:序号不在范围内 (256)"

"UnicodeEncodeError: 'latin-1' codec can't encode character u'\u015f' in position 2: ordinal not in range(256)"

上面标记的行.我做错了什么?

line marked above. What am i doing wrong?

推荐答案

尝试:

con = mdb.connect('loclhost', 'root', '', 'mydb', 
                  use_unicode=True, charset='utf8')

<小时>

这是一个证明它有效的演示:


Here is a demonstration showing that it works:

如果您没有在以下设置中使用 use_unicode=True,您将收到 UnicodeEncodeError:

If you do not use use_unicode=True with the following setup, you get a UnicodeEncodeError:

import MySQLdb
import config

def setup_charset(cursor, typ='latin1'):
    sql = 'DROP TABLE IF EXISTS foo'
    cursor.execute(sql)
    sql = '''\
        CREATE TABLE `foo` (
          `fooid` int(11) NOT NULL AUTO_INCREMENT,
          `bar` varchar(30),
          `baz` varchar(30),
          PRIMARY KEY (`fooid`)) DEFAULT CHARSET={t}
        '''.format(t=typ)
    cursor.execute(sql)
    sql = 'INSERT INTO foo (bar,baz) VALUES (%s,%s)'

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER,
    passwd=config.PASS, db='test')

cursor = connection.cursor()
setup_charset(cursor, typ='utf8')
sql = u'INSERT INTO foo (bar,baz) VALUES (%s,%s)'
try:
    cursor.execute(sql, [u'José Beitrge', u'∞'])
except UnicodeEncodeError as err:
    # You get this error if you don't use
    # (use_unicode=True, charset='utf8') see below.
    print(err)

引发异常:

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

虽然,如果您确实使用了 use_unicode=True,则可以插入 unicode 且不会出错:

While, if you do use use_unicode=True, you can insert unicode with no error:

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER,
    passwd=config.PASS, db='test',
    use_unicode=True,
    charset='utf8')
cursor = connection.cursor()
cursor.execute(sql, ['José Beitrge', '∞'])
cursor.execute('SELECT * from foo')
for row in cursor:
    print(u'{} {}'.format(*row[1:]))

印刷品

José Beitrge ∞

这篇关于Python unicode 编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SQL Server version of MySQL#39;s group_concat and escaped strings(MySQL 的 group_concat 和转义字符串的 SQL Server 版本)
How do I do a manual uninstall of Oracle?(如何手动卸载 Oracle?)
Can#39;t set root MySQL password to null(无法将 root MySQL 密码设置为 null)
Unable to start the mysql server in ubuntu(无法在 ubuntu 中启动 mysql 服务器)
MySQL won#39;t start after changin my.cf(更改 my.cf 后 MySQL 无法启动)
Unable to start MySQL server - Control process exited with error code(无法启动 MySQL 服务器 - 控制进程退出并显示错误代码)