Python源文件改写。编写一个程序,读取一个Python源程序文件source.py,将文件中所有除保留字外的小写字母换成大写字母。生成后的文件要能够被Python解释器正确执行。
我不知道还有什么其他方法可以从列表中把单词逐个分离出来
所以我用了jieba库
测试文件名:source.py(将此文件放在主程序相同目录)
def sum(num): result = 0 for x in range(1, num + 1): result += x return result print(sum(100))
主程序:
# -*- coding: utf-8 -*- """ @author:AragakiNoob @date:2019-5-10 """ import jieba import re import os; fo= open("source.py","r",encoding='utf-8').read() table=["def","for","in","return","print","range"] words = jieba.lcut(fo) fo2=open("source.py","w") pas='' for i in range(0,len(words)): if words[i] in table: pass; else: words[i]=words[i].upper() pas="".join(words) fo2.write(pas) fo2.close()
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com