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

PythonÈëÃŵÄ36¸öÀý×Ó Ö® 27

Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()

output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()

output:
e.g.3
ʵÏÖ¸ù¾ÝԭʼÎļþÓÐûÓÐ×îºóÒ»ÐпÕÐеÄÇé¿öÀ´½øÐГÍêÃÀÌí¼Ó”¡£
# 031
aFile = file(r'C:\temp.txt', 'r')
lastLine = ''
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
lastLine = line
# end of while
aFile.close()
aFile = file(r'C:\temp.txt', 'a')
if not lastLine.endswith('\n'): # ˵Ã÷Ô´ÎļþûÓÐÒ»¸ö¿ÕÐУ¬ÐèÒªÖØÐÂÁíÆðÒ»ÐÐ
aFile.write('\n')
# end of if
aFile.write('ÕâÊÇÐÂÌí¼ÓµÄÒ»ÐУ¡')
aFile.close()

´ËʱÎÞÂÛԭʼÎļþÊÇe.g.1µÄÑù×Ó»¹ÊÇe.g.2µÄÑù×Ó£¬½á¹û¶¼ÊÇ£º


Ïà¹ØÎĵµ£º

Python Re

1. Basic
²Î¿¼¡¶PythonÕýÔò±í´ïʽ²Ù×÷Ö¸ÄÏ¡· 
Ä£¿ére£¬perl·ç¸ñµÄÕýÔò±í´ïʽ
regex²¢²»Äܽâ¾öËùÓеÄÎÊÌ⣬ÓÐʱºò»¹ÊÇÐèÒª´úÂë
regex»ùÓÚÈ·¶¨ÐԺͷÇÈ·¶¨ÐÔÓÐÏÞ×Ô¶¯»ú
2. ×Ö·ûÆ¥Åä(Ñ­Ðò½¥½ø)
Ôª×Ö·û
. ^ $ * + ? { [ ] \ | ( )
1) "[" ºÍ "]"³£ÓÃÀ´Ö¸¶¨Ò»¸ö×Ö·ûÀà±ð£¬Ëùν×Ö·ûÀà±ð¾ÍÊÇÄãÏëÆ¥ÅäµÄÒ»¸ö×Ö·û¼¯¡£Èç[ ......

pythonÔËËã·û ¹©ÖØÔزο¼

¶þÔªÔËËã·û¼°Æä¶ÔÓ¦µÄÌØÊâ·½·¨
¶þÔªÔËËã·û
ÌØÊâ·½·¨
+
__add__,__radd__
-
__sub__,__rsub__
*
__mul__,__rmul__
/
__div__,__rdiv__,__truediv__,__rtruediv__
//
__floordiv__,__rfloordiv__
%
__mod__,__rmod__
**
__pow__,__rpow__
<<
__lshift__,__rlshift__
>>
_ ......

£¨×ª£©Python×Ö·û´®µÄencodeÓëdecode isinstance

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

PythonÈëÃŵÄ36¸öÀý×Ó——04 ÓÅÑŵÄ×Ö·û´®

# 004
# ÀûÓÃÈýÒýºÅ(''' or """)¿ÉÒÔָʾ¶àÐÐ×Ö·û´®
print '''line1
line2
line3'''
# ÁíÍ⣬Ä㻹¿ÉÒÔÔÚÈýÒýºÅÖÐÈÎÒâʹÓõ¥ÒýºÅºÍË«ÒýºÅ
print ''' "What's up? ," he replied.'''
# ·ñÔò£¬ÄãºÃʹÓÃתÒå·ûÀ´ÊµÏÖͬÑùµÄЧ¹û
# »¹ÊÇʹÓÃÈýÒýºÅºÃ£¬²»È»¾ÍÆÆ»µÁËÊÓ¾õÃÀÁË
print ' \"Wha ......

PythonÈëÃŵÄ36¸öÀý×Ó——17 Return

# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ