Python课后习题-第八章 读写文件
来源:互联网 发布:scientific linux安装 编辑:程序博客网 时间:2024/04/27 22:00
8.9.1 shelf 的删除-多重剪贴板
import shelve, pyperclip, sysmcbShelf = shelve.open('F:\\python\\mcb')if len(sys.argv) == 3 : if sys.argv[1].lower() == 'save' : mcbShelf[sys.argv[2]] = pyperclip.paste() print(pyperclip.paste()) elif sys.argv[1].lower() == 'delete': del mcbShelf[sys.argv[2]] #delete one item print('delete1')elif len(sys.argv) == 2 : if sys.argv[1].lower() == 'list' : pyperclip.copy(str(list(mcbShelf.keys()))) print(str(list(mcbShelf.keys()))) print(str(list(mcbShelf.values()))) elif sys.argv[1] in mcbShelf: pyperclip.copy(mcbShelf[sys.argv[1]]) print(mcbShelf[sys.argv[1]]) elif sys.argv[1].lower() == 'delete': mcbShelf.clear() #delele all print('delete2')mcbShelf.close()
8.9.2 疯狂填词
import rereadFile = open('F:\\python\\madlibs.txt')readContent = readFile.read()readFile.close()wordRegex = re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB')#正则表达式print(readContent)searchResult = wordRegex.findall(readContent)inputResult=[]for i in searchResult : if i == 'ADJECTIVE' : print('Enter an adjective:') inputResult.append(input()) if i == 'NOUN' : print('Enter a noun:') inputResult.append(input()) if i == 'ADVERB' : print('Enter a verb:') inputResult.append(input()) if i == 'VERB' : print('Enter an adverb:') inputResult.append(input())writeContent = wordRegex.sub('%s', readContent)#通配符插入writeFile = open('F:\\python\\madlibs_result.txt', 'w')writeFile.write(writeContent%(tuple(inputResult)))#通配符替换, tuple可以但是list不可以print(writeContent%(tuple(inputResult)))writeFile.close()
8.9.3 正则表达式查找 - 可以扩展 - grep
import os,sys,redef viewall(path): path = os.path.abspath(path) for i in os.listdir(path): #listdir() return relpath only file = os.path.join(path, i) #join to get abspath if os.path.isdir(file): viewall(file) elif os.path.basename(file).endswith('.txt'): checkfile(file)def checkfile(file): file = os.path.abspath(file) fileContent = open(file).readlines() for line in fileContent: if regex.search(line, re.I) != None : print(file + ':' + line)if len(sys.argv) != 3 : print('wrong format') sys.exit() rootPath = sys.argv[1]regex = re.compile(sys.argv[2])if not os.path.exists(rootPath): print('invalid path') sys.exit()if os.path.isdir(rootPath): viewall(rootPath)elif os.path.isfile(rootPath) and os.path.basename(rootPath).endswith('.txt'): checkfile(rootPath)
阅读全文
0 0
- Python课后习题-第八章 读写文件
- Java程序设计2 第八章课后习题
- 算法概论第八章课后习题
- 算法概论第八章课后习题
- 《Algorithms》第八章课后习题8.10题解
- 算法概论第八章课后习题8.8
- 算法概论第八章课后习题8.3
- C和指针课后习题(第八章)
- 【RFID第五次作业】第八章课后习题
- 【RFID第五次作业】第八章课后习题
- C++程序设计课后习题及解答(第八章)
- c primer plus 第八章 课后习题程序
- C++Primer(第八章课后习题程序题源代码)
- Python核心编程第二章课后习题
- python核心编程第五章课后习题
- Python 课后习题^-^
- 第八章 读写文件 笔记
- python学习之课后习题
- java的excel导出的简单示例
- WinDbg学习笔记十
- 机器学习-采用正态贝叶斯分类器对wine分类
- Studio导入多个module依赖aar出现丢包现象
- 晨星银华富裕主题数据抓取详细版
- Python课后习题-第八章 读写文件
- composer install php版本与composer.lock文件要求不一致
- Linux解决java.net.UnknownHostException
- 卷积神经网络CNN经典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning
- 服务器上模糊查询出现错误
- jquery lazyload 和 echo 两种方式实现图片延迟加载
- MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Com
- Ubuntu16+1080ti+cuda8+cudn7+caffe安装
- 【caffe】ubuntu16.04+CUDA8.0+OpenCV3.0+Caffe 安装记录