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

Python¶ÁдÎļþ


PythonÖÐÎļþ²Ù×÷¿ÉÒÔͨ¹ýopenº¯Êý£¬ÕâµÄÈ·ºÜÏñCÓïÑÔÖеÄfopen¡£Í¨¹ýopenº¯Êý»ñȡһ¸öfile object£¬È»ºóµ÷ÓÃread()£¬write()µÈ·½·¨¶ÔÎļþ½øÐжÁд²Ù×÷¡£
1.open
ʹÓÃopen´ò¿ªÎļþºóÒ»¶¨Òª¼ÇµÃµ÷ÓÃÎļþ¶ÔÏóµÄclose()·½·¨¡£±ÈÈç¿ÉÒÔÓÃtry/finallyÓï¾äÀ´È·±£×îºóÄܹرÕÎļþ¡£
file_object = open('thefile.txt')
try:
    all_the_text = file_object.read( )
finally:
    file_object.close( )
×¢£º²»ÄܰÑopenÓï¾ä·ÅÔÚtry¿éÀÒòΪµ±´ò¿ªÎļþ³öÏÖÒ쳣ʱ£¬Îļþ¶ÔÏófile_objectÎÞ·¨Ö´ÐÐclose()·½·¨¡£
2.¶ÁÎļþ
¶ÁÎı¾Îļþ
input = open('data', 'r')
#µÚ¶þ¸ö²ÎÊýĬÈÏΪr
input = open('data')
¶Á¶þ½øÖÆÎļþ: ´ò¿ª¶þ½øÖÆÎļþÒª¼Ó'b'±ê¼Ç
input = open('data', 'rb')
¶ÁÈ¡ËùÓÐÄÚÈÝ
file_object = open('thefile.txt')
try:
    all_the_text = file_object.read( )
finally:
    file_object.close( )
¶Á¹Ì¶¨×Ö½Ú
file_object = open('abinfile', 'rb')
try:
    while True:
        chunk = file_object.read(100)
        if not chunk:
            break
        do_something_with(chunk)
finally:
    file_object.close( )
¶ÁÿÐÐ
list_of_all_the_lines = file_object.readlines( )
Èç¹ûÎļþÊÇÎı¾Îļþ£¬»¹¿ÉÒÔÖ±½Ó±éÀúÎļþ¶ÔÏó»ñȡÿÐУº
for line in file_object:
    process line
3.дÎļþ
дÎı¾Îļþ
output = open('data', 'w')
д¶þ½øÖÆÎļþ
output = open('data', 'wb')
×·¼ÓдÎļþ
output = open('data', 'w+')
дÊý¾Ý
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )
дÈë¶àÐÐ
file_object.writelines(list_of_text_strings


Ïà¹ØÎĵµ£º

pythonÁ½¸ödictÏà¼Ó


>>> a = {'1':'2'}
>>> b = {'3':'4'}
>>> a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> a.update(b)
>>> a
{'1': '2', '3': '4'} ......

Python cPAMIE Ä£ÄâIEä¯ÀÀÆ÷

 
PAMIE ÊÇÒ»¸öºÜÓÐÒâ˼µÄÄ£¿é¡£ËüÊÇÒ»¸öʵÏÖIE×Ô¶¯»¯µÄÄ£¿é¡£ÔÚ´Ë֮ǰÎÒʹÓùýClientCookieÕâ¸öÄ£¿é£¬Ëü¿ÉÒÔʹÓÃurllib2À´Í¨¹ý³ÌÐò·ÃÎÊÒ³Ãæ£¬²¢ÇÒºÜÊʺÏÔÚÐèÒªcookieÖ§³ÖµÄ»·¾³ÖС£²»¹ý£¬ÓÐЩwebµÄ·ÃÎʹý³Ì²¢²»½ö½öÊÇÏÂÔØÎļþÕâÑùµÄÊÂÇ飬ÓÐʱÊÇһϵͳµÄ¶¯×÷¡£PAMIEÓ¦¸ÃÊÇÒ»¸ö¸üÖ±¹Û£¬¸ü¼òµ¥µÄÄ£ÄâÊÖ¹¤ÊäÈëµÄ¹ý³Ì¡£² ......

pythonѧϰ

3. Dictionaries ×ÖµäÀàÐÍ
     PythonÖУ¬×ÖµäÀàÐͲ¢²»ÊÇ˳ÐòÈÝÆ÷£¬¶øÀàËÆc++ÖеĹØÁªÈÝÆ÷(map)£¬DictionariesÖд洢µÄÊǼü/Öµ ¶Ô£¬ºÍmap²»Í¬µÄÊÇ£¬PythonµÄDictionariesÖпÉÒÔ´æÈÎÒâ¶ÔÏóÀàÐÍ¡£DictionariesÀàÐÍÒ²ÊǿɱäµÄ£¬ºÍListsÒ»Ñù£¬¿ÉÒÔÔ­µØÐ޸ģ¨Í¨¹ýϱêÐ޸ģ©¡£
    ÏÂÃæÍ¨ ......

Python Ö®ìø

import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough t ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ