Python±Ê¼Ç£¨9£©
PythonÖеÄÎļþ²Ù×÷ÒÔ¼°ÊäÈëÊä³ö
ÎÒÃÇ¿ÉÒÔ·Ö±ðʹÓÃraw_inputºÍprintÓï¾äÀ´Íê³ÉÕâЩ¹¦ÄÜ¡£¶ÔÓÚÊä³ö£¬ÄãÒ²¿ÉÒÔʹÓöàÖÖ¶àÑùµÄstr£¨×Ö·û´®£©Àà¡£ÀýÈ磬ÄãÄܹ»Ê¹ÓÃrjust·½·¨À´µÃµ½Ò»¸ö°´Ò»¶¨¿í¶ÈÓÒ¶ÔÆëµÄ×Ö·û´®¡£ÀûÓÃhelp(str)»ñµÃ¸ü¶àÏêÇé¡£
ÁíÒ»¸ö³£ÓõÄÊäÈë/Êä³öÀàÐÍÊÇ´¦ÀíÎļþ¡£´´½¨¡¢¶ÁºÍдÎļþµÄÄÜÁ¦ÊÇÐí¶à³ÌÐòËù±ØÐèµÄ£¬ÎÒÃǽ«»áÔÚÕâÕÂ̽Ë÷ÈçºÎʵÏÖÕâЩ¹¦ÄÜ¡£
Ò».ʹÓÃÎļþ
Äã¿ÉÒÔͨ¹ý´´½¨Ò»¸öfileÀàµÄ¶ÔÏóÀ´´ò¿ªÒ»¸öÎļþ£¬·Ö±ðʹÓÃfileÀàµÄread¡¢readline»òwrite·½·¨À´Ç¡µ±µØ¶ÁдÎļþ¡£¶ÔÎļþµÄ¶ÁдÄÜÁ¦ÒÀÀµÓÚÄãÔÚ´ò¿ªÎļþʱָ¶¨µÄģʽ¡£×îºó£¬µ±ÄãÍê³É¶ÔÎļþµÄ²Ù×÷µÄʱºò£¬Äãµ÷ÓÃclose·½·¨À´¸æËßPythonÎÒÃÇÍê³ÉÁ˶ÔÎļþµÄʹÓã¬ÔÚ¸÷·½Ã棬ÓëC/C++ÀàËÆ¡£
#!/usr/bin/python
# Filename: using_file.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print line,
# Notice comma to avoid automatic newline added by Python
f.close() # close the file
Êä³öΪ£º
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
ģʽ¿ÉÒÔΪ¶Áģʽ£¨'r'£©¡¢Ð´Ä£Ê½£¨'w'£©»ò×·¼Óģʽ£¨'a'£©¡£ÏêϸÇé¿ö¿É¼ûhelp(file)¡£
¶þ.´æ´¢Æ÷
PythonÌṩһ¸ö±ê×¼µÄÄ£¿é£¬³ÆÎªpickle¡£Ê¹ÓÃËüÄã¿ÉÒÔÔÚÒ»¸öÎļþÖд¢´æÈκÎPython¶ÔÏó£¬Ö®ºóÄãÓÖ¿ÉÒÔ°ÑËüÍêÕûÎÞȱµØÈ¡³öÀ´¡£Õâ±»³ÆÎª³Ö¾ÃµØ´¢´æ¶ÔÏó¡£
»¹ÓÐÁíÒ»¸öÄ£¿é³ÆÎªcPickle£¬ËüµÄ¹¦ÄܺÍpickleÄ£¿éÍêÈ«Ïàͬ£¬Ö»²»¹ýËüÊÇÓÃCÓïÑÔ±àдµÄ£¬Òò´ËÒª¿ìµÃ¶à£¨±Èpickle¿ì1000±¶£©¡£Äã¿ÉÒÔʹÓÃËüÃÇÖеÄÈÎÒ»¸ö£¬¶øÎÒÃÇÔÚÕâÀォʹÓÃcPickleÄ£¿é¡£¼Çס£¬ÎÒÃǰÑÕâÁ½¸öÄ£¿é¶¼¼ò³ÆÎªpickleÄ£¿é¡£
ÀýÈç:
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['a
Ïà¹ØÎĵµ£º
import sys
import os
import datetime
import time
class ArgsDealwith:
def arg_environment(self, args):
filepath = ('PYTHON_PATH', 'path')
for i in filepath:
&nbs ......
ÔÚ½²Êöfilter£¬mapºÍreduce֮ǰ£¬Ê×ÏȽéÉÜÒ»ÏÂÄäÃûº¯Êýlambda¡£
lambdaµÄʹÓ÷½·¨ÈçÏ£ºlambda [arg1[,arg2,arg3,...,argn]] : expression
ÀýÈ磺
>>> add = lambda x,y : x + y
>>> add ......
PythonµÄÃæÏò¶ÔÏóÐÔÖÊ
ÀàºÍ¶ÔÏóÊÇÃæÏò¶ÔÏó±à³ÌµÄÁ½¸öÖ÷Òª·½Ãæ¡£Àà´´½¨Ò»¸öÐÂÀàÐÍ£¬¶ø¶ÔÏóÕâ¸öÀàµÄ ʵÀý ¡£ÕâÀàËÆÓÚÄãÓÐÒ»¸öintÀàÐ͵ıäÁ¿£¬Õâ´æ´¢ÕûÊýµÄ±äÁ¿ÊÇintÀàµÄʵÀý£¨¶ÔÏ󣩡£
ÐèҪעÒâµÄÊÇ£¬ÔÚPythonÖУ¬¼´±ãÊÇÕûÊýÒ²±»×÷Ϊ¶ÔÏó£¨ÊôÓÚintÀࣩ¡£ÕâºÍC++¡¢Java£¨1.5°æÖ®Ç°£©°ÑÕûÊý´¿´â×÷ΪÀàÐÍÊDz»Í¬µÄ¡£Í¨¹ ......