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´úÂëÓÅ»¯--ÉÙ´ò×ÖС¼¼ÇÉ
˵Ã÷£ºÔö¼Ó´úÂëµÄÃèÊöÁ¦£¬¿ÉÒԳɱ¶¼õÉÙÄãµÄLOC£¬×öµ½¼òµ¥£¬²¢ÇÒÕæÇÐÓÐÁ¦
¹Ûµã£ºÉÙ´ò×Ö£½¶à˼¿¼£«ÉÙ³ö´í£¬10´úÂëÐбÈ50ÐиüÄÜÈÃÈËÃ÷°×£¬ÒÔϼ¼ÇÉÓÐÖúÓÚÌá¸ß5±¶¹¤×÷ЧÂÊ
1. ½»»»±äÁ¿ÖµÊ±±ÜÃâʹÓÃÁÙʱ±äÁ¿£º(cookbook1.1)
ÀÏ´úÂ룺ÎÒÃǾ³£ºÜÊìÁ·ÓÚÏÂÃæµÄ´úÂë
temp = x
x = y
y = ......
Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö·û´®×ª»»³ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# ÁбíµÄºÏ²¢
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# ÅжÏÁбíÖÐÊÇ·ñ°üº¬Ä³ÔªËØ
print 'b' in list1
print 1 in list1
# ɾ³ýij¸öÔªËØ
for ele in ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 025
# ÐòÁеÄÉñÆæÖ®´¦ÔÚÓÚÄã¿ÉÒÔʹÓÃÏàͬµÄ·½Ê½tuple¡¢listºÍstring
se = ['a', 'b', 'c', 'd']
li = ['a', 'b', 'c', 'd']
tu = ('a', 'b', 'c', 'd')
string = 'abcd'
print se[1]
print li[1]
print tu[1]
print string[1]
# ÐòÁеÄÁíÍâÒ»¸öÉñÆæÖ®´¦ÔÚÓÚ£¬Äã¿ÉÒÔʹÓøºÊý½øÐÐË÷ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 026
aList = ['1','2','3','4']
aListCopy = aList # Æäʵ£¬ÕâÀï½ö½ö¸´ÖÆÁËÒ»¸öÒýÓÃ
del aList[0]
print aList
print aListCopy # Á½¸öÒýÓÃÖ¸ÏòÁËÁËͬһ¸ö¶ÔÏó£¬ËùÒÔ´òÓ¡½á¹ûÒ»Ñù
aListCopy = aList[:] # ÕâÊǸ´ÖÆÕû¸ö¶ÔÏóµÄÓÐЧ·½·¨
del aList[0]
print aList
print aListCopy
......