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

Python×Ö·û¼¯±àÂëºÍÎļþ¶Áд

 ÕâƪÎÄÕ½²µÃ±È½ÏÇå³þpythonµÄ×Ö·û´®±àÂëÎÊÌâ
Ô­Îijö´¦:http://hi.baidu.com/yobin/blog/item/894158b575090dcb37d3ca07.html
------------------------------------------------------------------
×Ö·û´®±àÂë
pythonÖÐĬÈϱàÂëÊÇASCII£¬¿ÉÒÔͨ¹ýÒÔÏ·½Ê½ÉèÖúͻñÈ¡£º
import sys
print sys.getdefaultencoding()
sys.setdefaultencoding('gbk')
µ«Ö±µ½pythonÖØÐÂÆô¶¯ºóеÄĬÈϱàÂë²Å»áÉúЧ£¬ÎÒÊÔÁËһϣ¬setdefaultencoding×ÜÊÇ»á³ö´í£¬Ã»ÓÐÕâ¸öÊôÐÔ¡£ÓÃdir¿´£¬È·ÊµÃ»ÓУ¬python°æ±¾ÊÇ2.5£¬²»ÖªµÀÊÇ·ñ±»È¡ÏûÁË¡£
ʹÓÃprintÀ´Êä³öʱ£¬python½«ÄÚÈÝ´«µÝ¸øϵͳ´¦Àí£¬windows»á°´ÕÕϵͳĬÈϱàÂëÀ´Êä³ö¡£Èç¹û°üº¬ÁËÖÐÎÄ£¬¾ÍҪעÒ⼸µã¡£
1 python´úÂëÎļþµÄ±àÂë
pyÎļþĬÈÏÊÇASCII±àÂ룬ÖÐÎÄÔÚÏÔʾʱ»á×öÒ»¸öASCIIµ½ÏµÍ³Ä¬ÈϱàÂëµÄת»»£¬Õâʱ¾Í»á³ö´í£ºSyntaxError: Non-ASCII character¡£ÐèÒªÔÚ´úÂëÎļþµÄµÚÒ»ÐлòµÚ¶þÐÐÌí¼Ó±àÂëָʾ£º
1 # coding=gbk
2 print 'ÖÐÎÄ'
2 ×Ö·û´®µÄ±àÂë
ÏñÉÏÃæÄÇÑùÖ±½ÓÊäÈëµÄ×Ö·û´®ÊÇ°´ÕÕ´úÂëÎļþµÄ±àÂëÀ´´¦ÀíµÄ£¬Èç¹ûÊÇunicode±àÂ룬ÓÐÒÔÏÂÈýÖÖ·½Ê½£º
1 s1 = u'ÖÐÎÄ'
2 s2 = unicode('ÖÐÎÄ','gbk')
3 s3 = s1.decode('gbk')
unicodeÊÇÒ»¸öÄÚÖú¯Êý£¬µÚ¶þ¸ö²ÎÊýָʾԴ×Ö·û´®µÄ±àÂë¸ñʽ¡£
decodeÊÇÈκÎ×Ö·û´®¾ßÓеķ½·¨£¬½«×Ö·û´®×ª»»³Éunicode¸ñʽ£¬²ÎÊýָʾԴ×Ö·û´®µÄ±àÂë¸ñʽ¡£
encodeÒ²ÊÇÈκÎ×Ö·û´®¾ßÓеķ½·¨£¬½«×Ö·û´®×ª»»³É²ÎÊýÖ¸¶¨µÄ¸ñʽ¡£
3 ϵͳµÄĬÈϱàÂë
¶Ô ÓÚÖÐÎÄϵͳÀ´Ëµ£¬Ä¬ÈϵÄÊÇgbk£¬gb2312Ò²¿ÉÒÔ£¬ÒòΪËüÊÇgbkµÄ×Ö¼¯¡£Ê¹ÓÃprintÊä³öʱ£¬×Ö·û´®»á±»×ª»»³É´Ë¸ñʽ£¬Òþʽת»»Ê±£¬ÊÇ´Ó´úÂëÎļþ ±àÂë¸ñʽת»»³Égbk£¬Ä¬ÈÏÊÇASCII->GBK¡£¿¼ÂÇÉÏÃæµÚ¶þµã£¬Èç¹û×Ö·û´®±àÂë²»ÊÇASCII£¬ÔòÒþʽת»»»á³ö´í£¬ÐèÒªÏÔʽת»»£¬Ê¹Óà encode·½·¨¡£Èç¹ûÖ¸¶¨ÁË´úÂëÎļþ¸ñʽΪgbk£¬ÔòÒþʽת»»²»´æÔÚÎÊÌâ¡£
1 # coding=gbk

3 s = u'ÖÐÎÄ'
4 print s.encode('gbk')
Îļþ¶Áд
Ö»ÊÇASCII»òÕßgbk±àÂë¸ñʽµÄµÄÎļþ¶Áд£¬±È½Ï¼òµ¥£¬¶ÁдÈçÏ£º
1 # coding=gbk

3 f = open('c:/intimate.txt','r') # r ָʾÎļþ´ò¿ªÄ£Ê½£¬¼´Ö»¶Á
4 s1 = f.read()
5 s2 = f.readline()
6 s3 


Ïà¹ØÎĵµ£º

eat python 003

Documentation for C's fopen():
---
r Open text file for reading. The stream is positioned at the beginning
of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing. The
stream is posi ......

Eclipse+PyDev = Python±à¼­Æ÷£¬

http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/Á¬½ÓÕâ¸öÍøÖ·£¬²»»áÈÃÄãʧÍûµÄ¡£
PyDev ¼ò½é
2003Äê7ÔÂ16ÈÕ£¬ÒÔ Fabio Zadrozny ΪÊ×µÄÈýÈË¿ª·¢Ð¡×éÔÚÈ«Çò×î´óµÄ¿ª·ÅÔ´´úÂëÈí¼þ¿ª·¢Æ½Ì¨ºÍ²Ö¿â SourceForge ÉÏ×¢²áÁËÒ»¿îеÄÏîÄ¿£¬¸ÃÏîĿʵÏÖÁËÒ»¸ö¹¦ÄÜÇ¿´óµÄ Eclipse²å¼þ£¬Óû§¿ÉÒÔÍêÈ«ÀûÓà Eclipse À ......

python ÖÐÎÄÂÒÂë ÎÊÌâÉîÈë·ÖÎö

 
ת×Ô:http://hi.baidu.com/%C1%EE%BA%FC%CF%F3/blog/item/efb76fb7f0411dc437d3ca20.html
 
ÔÚ±¾ÎÄÖУ¬ÒÔ'¹þ'À´½âÊÍ×÷ʾÀý½âÊÍËùÓеÄÎÊÌ⣬“¹þ”µÄ¸÷ÖÖ±àÂëÈçÏ£º
  1. UNICODE (UTF8-16)£¬C854£»
  2£® UTF-8£¬E59388£»
  3£® GBK£¬B9FE¡£
Ò»¡¢pythonÖÐ ......

pythonÖÐÎÄĿ¼

 ÈçÓÐһ·¾¶"e:/software/²âÊÔ.txt" 
ÐèҪͨ¹ýpyhon¶Á/д "²âÊÔ.txt"Îļþ£¬¿ÉÒÔ²ÉÓÃÏÂÃæµÄ·½·¨£º 
ipath = "e:/software/²âÊÔ.txt" 
uipath = unicode(ipath , "utf8") 
È»ºóÓÃ"uipath"¾­¹ý±àÂëºóµÄ·¾¶È¥open()¼´¿É¡£ 
ÏÂÃæÊÇÎÒÔÚÏîÄ¿ÖÐÓÃpython¶ÁдexcelµÄÒ»µãÌå»á(½â¾öÁ˶ÁÖÐÎÄÎļþà ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ