Python Coding Problem Solution

Source: Internet
Author: User

1.UnicodeDecodeError: ' ASCII ' codec can ' t decode byte 0xe4 in position 0:ordinal not in range (128)

Solution:

Import Sys
Reload (SYS)
Sys.setdefaultencoding (' utf-8 ')

2.unicodeencodeerror: ' latin-1 ' codec can ' t encode characters in position 9-13:ordinal not in range

Solution:

Add the following arrows to the indicated sentence, which is the blue code behind the figure

account = Accountraw.encode ("Utf-8"). Decode ("Latin1")

3.python2.7 query or Insert Chinese data in MySQL when there is a Chinese garbled

---Possible conditions:1.mysql Database items are not set encoding, default to ' Latin '2. The default encoding is not set when using Mysql.connect3. Python encoding is not set, python2.7 defaults to ' ASCII '4. No decoding---Workaround:1. Set the MySQL encoding

Ubuntu executes the following statements:
* * sudo vim/etc/mysql/my.cnf * *
Then insert the statement inside:

[client]default-character-set=utf8[mysqld]character-set-server=utf8collation-server=utf8_general_ci       

Exit vim
Restart MySQL:
* * sudo service mysql restart * *

2. Set the MYSQLDB connection encoding parameter in code
Db=mysqldb.Connect (user= ' ... ',db= ' ...', passwd= ' ... ',host=' ... ', charset=' UTF8 ')   
3. Set python default encoding in code
# -*-coding:utf-8 -*-import sys reload(sys)sys.setdefaultencoding(‘utf-8‘)
4. Remember to decode
t = cursor.fetchall()s = t[0][1].decode(‘utf-8‘)
Over

Python Coding Problem Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.