Today with the Python-mysqldb module inserted in Chinese, the collection to Nginx, IP address analysis and then warehousing processing, to make a graph analysis, inserted in the Chinese language encountered a small problem:
The initial format is this:
[[Email protected] python]# cat queryadd.py #!/usr/bin/env python#coding=utf-8 import jsonimport mysqldb def getaddress (): pvdic={} f = open ('/tmp/output.txt ', ' R ') n = 0 for line in f: add = Line.split () addressname=add[1].strip (' \ n ') conn=mysqldb.connect (host= ' 192.168.10.205 ', user= ' test ', passwd= ' 123456 ', db= ' Yizhan ', port=3306,use_unicode=1,charset= "UTF8") cursor= Conn.cursor () cursor.execute ("Insert into app01_map ( Address) values (%s) "% (Addressname)) conn.commit () &nbSp;cursor.close () conn.close () print addressname f.close () if __name__== ' __main__ ': getaddress ()
This format, inserting numbers and English is not a problem, but inserting Chinese is an error: So here is the change:
[email protected] python]# cat test_mysql.py #!/usr/bin/env python#coding=utf-8 import jsonimport mysqldb def getaddress (): Addressname= "Nanning" Conn=mysqldb.connect (host= ' 192.168.10.205 ', user= ' test ', passwd= ' 123456 ', db= ' Yizhan ', port= 3306,use_unicode=1,charset= "UTF8") cursor=conn.cursor () query = "INSERT into App01_map (address) values (%s)" Curso R.execute (Query,addressname) conn.commit () Cursor.close () conn.close () print addressname if __name__== ' __main__ ': getaddress ()
Insert success:
[email protected] python]# python test_mysql.py
Nanning
This article is from the "Little Luo" blog, please be sure to keep this source http://xiaoluoge.blog.51cto.com/9141967/1653405
About MySQL module cannot insert Chinese question