标签:style blog http color 使用 os strong 文件
1 #coding:utf-8 2 fn=‘test1.py‘ 3 fp=open(fn,‘r‘) #以读的方式打开文件,文件必须首先存在和,.文件在同一目录下py 4 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 r 13 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 aaaaaaaa 3 bbbbbbbb 4 cccccccc
1 fr=open(‘wformat.txt‘,‘r‘) 2 line1=fr.readline() 3 print line1 4 line2=fr.readline() 5 print line2 6 print line2.split(‘\t‘) 7 fr.close()
结果如下:
1 >>> 2 name age sex 3 4 张三 78 male 5 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 sex 13 14 张三 78 male 15 16 李四 50 male 17 18 王五 80 male 19 20 张强 90 female 21 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 dict1 3 {‘a‘: ‘hello‘, 12: 34, ‘name‘: ‘jeap‘}
4)元素项的删除:
1 >>> del dict1[12] 2 >>> print dict1 3 {‘a‘: ‘hello‘, ‘name‘: ‘jeap‘}
5)元素项的增加:
1 >>> dict1[‘QQ‘]=‘649414754‘ 2 >>> print dict1 3 {‘a‘: ‘hello‘, ‘QQ‘: ‘649414754‘, ‘name‘: ‘jeap‘} 4 >>> dict1[‘sex‘]=‘F‘ 5 >>> print dict1 6 {‘a‘: ‘hello‘, ‘QQ‘: ‘649414754‘, ‘name‘: ‘jeap‘, ‘sex‘: ‘F‘}
6)in运算:
1 >>> ‘name‘ in dict1 2 True 3 >>> ‘F‘ in dict1 4 False
1 >>> print dict1 2 {‘a‘: ‘hello‘, ‘QQ‘: ‘649414754‘, ‘name‘: ‘jeap‘, ‘sex‘: ‘F‘} 3 >>> dict1.clear() 4 >>> print dict1 5 {}
1 >>> dict1={‘a‘: ‘hello‘, ‘QQ‘: ‘649414754‘, ‘name‘: ‘jeap‘, ‘sex‘: ‘F‘} 2 >>> dict2=dict1.copy() 3 >>> print dict2 4 {‘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 dict1 6 {‘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 d2 13 {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 dic 11 {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 Jack 10 Rose 11 John 12 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 ]= Rose 10 dic[ 4 ]= John 11 dic[ 5 ]= Mark 12 >>>
五,小结
本章主要介绍python开发的进阶知识,文件的基本操作,字典的相关概念,基本操作运算和相关函数,为以后实战应用做一个铺垫,本章存在的遗留问题是,如何调用不在同一目录文件下的.py自定义模块?按照书上的代码未能实现。
Python学习系列(五)(文件操作及其字典),布布扣,bubuko.com
标签:style blog http color 使用 os strong 文件
原文地址:http://www.cnblogs.com/zhangbc/p/3895944.html