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自定义模块?按照书上的代码未能实现。
新闻热点
疑难解答