Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

PythonÁ¬½ÓMySQL


°æÈ¨ÉùÃ÷
£º×ªÔØÊ±ÇëÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕÂԭʼ³ö´¦ºÍ×÷ÕßÐÅÏ¢¼°±¾ÉùÃ÷
http://mobile2008.blogbus.com/logs/28725962.html
 
°²×°ºÃÖ®ºó£¬Ä£¿éÃû×Ö½Ð×öMySQLdb £¬ÔÚWindowºÍLinux»·¾³Ï¶¼¿ÉÒÔʹÓã¬ÊÔÑéÁËÒ»ÏÂͦºÃÓã¬
²»¹ýÓÖ·¢ÏÖÁË·³È˵ÄÂÒÂéÎÊÌ⣬×îºóÓÃÁ˼¸¸ö°ì·¨£¬½â¾öÁË£¡
ÎÒÓÃÁËÏÂÃæ¼¸¸ö´ëÊ©£¬±£Ö¤MySQLµÄÊä³öûÓÐÂÒÂ飺
    1 PythonÎļþÉèÖñàÂë utf-8 £¨ÎļþÇ°Ãæ¼ÓÉÏ #encoding=utf-8)
    2 MySQLÊý¾Ý¿âcharset=utf-8
    3 PythonÁ¬½ÓMySQLÊǼÓÉϲÎÊý charset=utf8
    4 ÉèÖÃPythonµÄĬÈϱàÂëΪ utf-8 (sys.setdefaultencoding(utf-8)
mysql_test.py
#
encoding=utf-8
import
 sys
import
 MySQLdb
reload(sys)
sys.setdefaultencoding(
'
utf-8
'
)
db
=
MySQLdb.connect(user
=
'
root
'
,charset
=
'
utf8
'
)
cur
=
db.cursor()
cur.execute(
'
use mydb
'
)
cur.execute(
'
select * from mytb limit 100
'
)
f
=
file(
"
/home/user/work/tem.txt
"
,
'
w
'
)
for
 i 
in
 cur.fetchall():
    f.write(str(i))
    f.write(
"

"
)
f.close()
cur.close()
ÉÏÃæÊÇlinuxÉϵĽű¾,windowsÏÂÔËÐÐÕý³£!
×¢£ºMySQLµÄÅäÖÃÎļþÉèÖÃÒ²±ØÐëÅäÖóÉutf8
ÉèÖàMySQL µÄ my.cnf Îļþ£¬ÔÚ [client]/[mysqld]²¿·Ö¶¼ÉèÖÃĬÈϵÄ×Ö·û¼¯£¨Í¨³£ÔÚ/etc/mysql/my.cnf)£º
[client]
default-character-set = utf8
[mysqld]
default-character-set = utf8
£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­£­
#!/usr/bin/env python
# -*-coding:UTF-8-*-#ÕâÒ»¾ä¸æËßpythonÓÃUTF-8±àÂë
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE  : 2004-12-28
#
# COMMENT: ÕâÊÇÒ»¸öpythonÁ¬½ÓmysqlµÄÀý×Ó
#
#=========================================================================
"""
 ***** This is a MySQL test *****
 
 select:
  conn=


Ïà¹ØÎĵµ£º

python os Ä£¿é

Python 3 ½Ì³Ì¶þ:Îļþ,Ŀ¼ºÍ·¾¶
http://www.cnitblog.com/yunshichen/archive/2009/04/01/55931.html
python osÄ£¿é
http://hi.baidu.com/happynp/blog/item/729243f902d5a751242df2c2.html
http://hi.baidu.com/fiber212121/blog/item/6e07ec03c97b6982d53f7c27.html
python getoptÄ£¿é
http://www.tsnc.edu.cn/de ......

Python ×Ö·û´®·½·¨Ïê½â


ÀàÐÍ
·½·¨
×¢½â
Ìî³ä
center(width[, fillchar]) ,
ljust(width[, fillchar]),
rjust(width[, fillchar]),
zfill(width),
expandtabs([tabsize])
l        
fillchar ²ÎÊýÖ¸¶¨ÁËÓÃÒÔÌî³äµÄ×Ö·û£¬Ä¬ÈÏΪ¿Õ¸ñ
l        
¹Ë ......

Python×Ö·û´®µÄencodeÓëdecode


Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö·û´®×ª» ......

ÓÃCÓïÑÔÀ©Õ¹PythonµÄ¹¦ÄÜ


Ô­ÎÄ
PythonºÍC·Ö±ðÓÐן÷×ÔµÄÓÅȱµã£¬ÓÃPython¿ª·¢³ÌÐòËٶȿ죬¿É¿¿ÐԸߣ¬²¢ÇÒÓÐÐí¶àÏÖ³ÉÄ£¿é¿É¹©Ê¹Ó㬵«Ö´ÐÐËÙ¶ÈÏà¶Ô½ÏÂý£»CÓïÑÔÔòÕýºÃÏà·´£¬ÆäÖ´ÐÐËٶȿ죬µ«¿ª·¢Ð§Âʵ͡£ÎªÁ˳ä·ÖÀûÓÃÁ½ÖÖÓïÑÔ¸÷×ÔµÄÓŵ㣬±È½ÏºÃµÄ×ö·¨ÊÇÓÃPython¿ª·¢Õû¸öÈí¼þ¿ò¼Ü£¬¶øÓÃCÓïÑÔʵÏ֯乨¼üÄ£¿é¡£±¾ÎĽéÉÜÈçºÎÀûÓÃCÓïÑÔÀ´À©Õ¹PythonµÄ¹¦ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ