文件src.txt存储的是一篇英文文章, 将其中所有大写字母转换成小写字母存入文件dest.txt。
f = open('src.txt', 'r')
text = f.read()
lines = [line.lower() for line in text]
with open('dest.txt', 'w') as out:
out.writelines(lines)
运行结果:
$ cat src.txt
f = OPEN('src.txt', 'r')
text = f.READ()
lines = [line.LOWER() FOR line IN text]
WITH OPEN('dest.txt', 'w') AS OUT:
OUT.WRITELINES(lines)
$ python3 lower.py
$ cat dest.txt
f = open('src.txt', 'r')
text = f.read()
lines = [line.lower() for line in text]
with open('dest.txt', 'w') as out:
out.writelines(lines)
$
如果用UNIX命令就很简单
tr [:upper:] [:lower:] <src.txt >dest.txt