[Python]utf8文本内容无法插入到mysql数据库中

文本头部设置了

#coding: utf-8

但是当你处理这些utf8文本的时候, 还是依然会出现”acsii balabala”之类的问题. 这个时候你需要在文件顶部在加入三行代码

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

这个时候 就能正确处理了. 原因很简单python2.7及以下版本(3.0不知道), 如果不那么申明, 内部依赖还是以ascii码处理参数的.

Leave a Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: