文章目录
- 题目
- 代码
- 运行后效果
题目
Python源文件改写。编写一个程序,读取一个Python源程序文件source.py,将文件中所有除保留字外的小写字母换成大写字母。
代码
import keyword
import jieba
from tkinter import filedialog
f_path = filedialog.askopenfilename()
keywords = keyword.kwlist
print(keywords)
file_read = open(f_path,"r",encoding="UTF-8").read()
file_write = open(f_path,"w",)
all_words = jieba.lcut(file_read)
str_words = ""
for i in range(0, len(all_words)):
if all_words[i] in keywords:
pass
else:
all_words[i] = all_words[i].upper()
str_words = "".join(all_words)
file_write.write(str_words)
file_write.close()
运行后效果