久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python unicode 編碼問題

Python unicode encoding issue(Python unicode 編碼問題)
本文介紹了Python unicode 編碼問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

使用 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é Beitr?ge', 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é Beitr?ge', '∞'])
cursor.execute('SELECT * from foo')
for row in cursor:
    print(u'{} {}'.format(*row[1:]))

印刷品

José Beitr?ge ∞

這篇關于Python unicode 編碼問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Typeorm Does not return all data(Typeorm 不返回所有數據)
Dynamically creating a placeholder to insert many column values for a row in SQLite table(動態創建占位符以在 SQLite 表中為一行插入多個列值)
MySQL return extra records when using a long type number to filter varchar type(MySQL在使用長類型數字過濾varchar類型時返回額外記錄)
MySQL Error #1071 - Specified key was too long; max key length is 767 bytes(MySQL 錯誤 #1071 - 指定的鍵太長;最大密鑰長度為 767 字節)
MySQL command-line table column width with utf8(MySQL命令行表列寬與utf8)
Create a MySQL stored function with a dynamic number of arguments(創建一個帶有動態參數數量的 MySQL 存儲函數)
主站蜘蛛池模板: 毛片免费视频 | 91麻豆精品一区二区三区 | 极品粉嫩国产48尤物在线播放 | 亚洲欧美网站 | 国内精品视频在线观看 | 黑人性hd| 高清av在线 | 欧美aaa一级片 | 美女一区 | 国产精品日韩欧美一区二区三区 | 成人自拍视频网站 | 国产午夜影院 | 国产片侵犯亲女视频播放 | 久久大陆| 亚洲精品久久久一区二区三区 | 久久精品国产一区二区电影 | 免费午夜视频在线观看 | 久久久国产一区二区 | 国产精品成人一区二区三区吃奶 | 操亚洲 | 亚洲人人 | aaaa网站 | 狠狠久| 久久精品小视频 | 四虎在线观看 | 日韩欧美在线不卡 | 国产精品国产三级国产aⅴ无密码 | 国户精品久久久久久久久久久不卡 | 精精国产xxxx视频在线播放 | 国产精品一区二区三 | 欧美激情视频一区二区三区在线播放 | 国产成都精品91一区二区三 | 久久天堂网 | 中文字幕不卡在线观看 | 日本网站在线看 | 国产欧美精品区一区二区三区 | 久久国产精品视频免费看 | 在线观看av免费 | 久久久一二三 | 夜夜爆操| 成人在线网址 |