【发布时间】:2017-11-22 14:53:43
【问题描述】:
我正在尝试编写一个 python 脚本来格式化 SQL 代码以获得更好的可读性。
例如。将特定的小写单词转换为大写:select -> SELECT
我正在尝试使用 Python 中的读写功能来做到这一点。但是,我被困住了。这是我的代码:
words = ['select', 'from', 'where']
w = open('03_TextUpper.txt', 'w')
with open('03_TextLower.txt', 'r') as file:
for line in file:
for word in line.split():
if word in words:
w.write( word.upper() )
w.write( line )
这会打印出特定单词的大写字母,但不会删除小写单词。
有没有更好的方法用 Python 编写这个?
【问题讨论】:
标签: python python-3.x text