
1 #coding:utf-82 fn='test1.py'3 fp=open(fn,'r') #以讀的方式打開文件,文件必須首先存在和,.文件在同一目錄下py4 PRint 'reading pos:',fp.tell()5 r=fp.read(20) #讀取文件內容返回字符串6 print 'have read:/"'+r+'/''7 print 'reading pos:',fp.tell()8 print fp.read()9 fp.close()
2,寫文件
如果想將某些內容寫入文件,可以以'w'寫的方式打開文件(如果文件不存在會創建),并且清空文件之前的內容。

1 fw='test.txt' 2 fp=open(fw,'w') 3 fp.write('www.google.com') 4 fp.close()

1 fn='rplus.txt'2 fp=open(fn,'w+') 3 r=fp.read(12) 4 print r 5 fp.close()

1 fn='rplus.txt' 2 fp=open(fn,'w+') 3 fp.write('aaaaa/n') 4 fp.close() 5 6 fa=open('rplus.txt','a') 7 fa.write('bbbbb/n') 8 fa.close() 9 10 fa=open(fn,'r')11 r=fa.read()12 print r13 fa.close()
二,格式化讀寫文件

1 fn='wformat.txt'2 fw=open(fn,'w')3 fw.write('%10s/t %3s/t %6s/n'%('name','age','sex'))4 fw.write('%10s/t %3s/t %6s/n'%('張三',78,'male'))5 fw.write('%10s/t %3s/t %6s/n'%('李四',50,'male'))6 fw.write('%10s/t %3s/t %6s/n'%('王五',80,'male'))7 fw.write('%10s/t %3s/t %6s/n'%('張強',90,'female'))8 fw.close()

1 fr=open('templist.txt','r')2 print fr.readlines()3 fr.close()

1 >>>2 [' aaaaaaaa/n', ' bbbbbbbb/n', ' cccccccc']

1 fr=open('templist.txt','r')2 print fr.readline().strip().strip('/n')3 print fr.readline().strip().strip('/n')4 print fr.readline().strip().strip('/n')5 fr.close()
結果如下:

1 >>>2 aaaaaaaa3 bbbbbbbb4 cccccccc

1 fr=open('wformat.txt','r')2 line1=fr.readline()3 print line14 line2=fr.readline()5 print line26 print line2.split('/t')7 fr.close()
結果如下:

1 >>>2 name age sex3 4 張三 78 male5 6 [' /xd5/xc5/xc8/xfd', ' 78', ' male/n']
讀取文件(格式化)的內容:

1 fr=open('wformat.txt','r') 2 while (1==1): 3 line=fr.readline() 4 if(line==''): 5 break 6 else: 7 print line 8 fr.close() 9 10 >>> ================================ RESTART ================================11 >>>12 name age sex13 14 張三 78 male15 16 李四 50 male17 18 王五 80 male19 20 張強 90 female21 22 >>>

1 fn='c://test.txt' 2 fp=open(fn,'w+') 3 fp.write('www.python.com') 4 fp.close()

1 >>> dict1={'a':'b','name':'jeap',12:34}2 >>> len(dict1)3 3

1 >>> dict1={'a':'b','name':'jeap',12:34}2 >>> print dict1['a'],dict1[12]3 b 34
3)元素值的修改:

1 >>> dict1['a']='hello'2 >>> print dict13 {'a': 'hello', 12: 34, 'name': 'jeap'}
4)元素項的刪除:

1 >>> del dict1[12]2 >>> print dict13 {'a': 'hello', 'name': 'jeap'}
5)元素項的增加:

1 >>> dict1['QQ']='649414754'2 >>> print dict13 {'a': 'hello', 'QQ': '649414754', 'name': 'jeap'}4 >>> dict1['sex']='F'5 >>> print dict16 {'a': 'hello', 'QQ': '649414754', 'name': 'jeap', 'sex': 'F'}
6)in運算:

1 >>> 'name' in dict12 True3 >>> 'F' in dict14 False

1 >>> print dict12 {'a': 'hello', 'QQ': '649414754', 'name': 'jeap', 'sex': 'F'}3 >>> dict1.clear()4 >>> print dict15 {}

1 >>> dict1={'a': 'hello', 'QQ': '649414754', 'name': 'jeap', 'sex': 'F'}2 >>> dict2=dict1.copy()3 >>> print dict24 {'a': 'hello', 'QQ': '649414754', 'name': 'jeap', 'sex': 'F'}

1 {'a': 'hello', 'QQ': '649414754', 'name': 'jeap', 'sex': 'F'}2 >>> dict1.get('QQ')3 '649414754'

1 >>> dict1.keys()2 ['a', 'QQ', 'name', 'sex']

1 >>> dict1.values()2 ['hello', '649414754', 'jeap', 'F']

1 >>> dict1.items()2 [('a', 'hello'), ('QQ', '649414754'), ('name', 'jeap'), ('sex', 'F')]

1 >>> new={'age':32} #原字典沒有,新增2 >>> add={'name':'張三'} #原字典存在,更新'jeap'為'張三'3 >>> dict1.update(new)4 >>> dict1.update(add)5 >>> print dict16 {'a': 'hello', 'QQ': '649414754', 'name': '/xd5/xc5/xc8/xfd', 'age': 32, 'sex': 'F'}

1 >>> d0=dict() #創建空字典 2 >>> print d0 3 {} 4 >>> d1=dict(name='zhangsan',QQ='123456789',age=23)#通過賦值創建字典 5 >>> print d1 6 {'QQ': '123456789', 'age': 23, 'name': 'zhangsan'} 7 >>> val=['lisi','649414754',25] 8 >>> print val 9 ['lisi', '649414754', 25]10 >>> key=range(1,4)11 >>> d2=dict(zip(key,val))#使用一對列表創建字典12 >>> print d213 {1: 'lisi', 2: '649414754', 3: 25}

1 >>> val=['Tom','Jack','Rose','John','Mark'] 2 >>> key=range(1,6) 3 >>> dic=dict(zip(key,val)) 4 >>> print dic 5 {1: 'Tom', 2: 'Jack', 3: 'Rose', 4: 'John', 5: 'Mark'} 6 >>> dic.pop(2) 7 'Jack' 8 >>> dic.popitem() 9 (1, 'Tom')10 >>> print dic11 {3: 'Rose', 4: 'John', 5: 'Mark'}

1 >>> key=range(1,6) 2 >>> val=['Tom','Jack','Rose','John','Mark'] 3 >>> dic=dict(zip(key,val)) 4 >>> for x in dic: 5 print dic[x] 6 7 8 Tom 9 Jack10 Rose11 John12 Mark

1 >>> print dic.items() 2 [(1, 'Tom'), (2, 'Jack'), (3, 'Rose'), (4, 'John'), (5, 'Mark')] 3 >>> for (k,v) in dic.items(): 4 print 'dic[',k,']=',v 5 6 7 dic[ 1 ]= Tom 8 dic[ 2 ]= Jack 9 dic[ 3 ]= Rose10 dic[ 4 ]= John11 dic[ 5 ]= Mark12 >>>
五,小結
本章主要介紹python開發的進階知識,文件的基本操作,字典的相關概念,基本操作運算和相關函數,為以后實戰應用做一個鋪墊,本章存在的遺留問題是,如何調用不在同一目錄文件下的.py自定義模塊?按照書上的代碼未能實現。
新聞熱點
疑難解答