将文件中的所有文本更改为小写
问题描述:
from itertools import chain
from glob import glob
file = open('FortInventory.txt','w')
lines = [line.lower() for line in lines]
with open('FortInventory.txt', 'w') as out:
out.writelines(sorted(lines))
我想将txt文件中的所有文本转换为小写如何执行此操作,这里是我迄今为止的代码,并且我查看了堆栈上的一些问题溢出,但我无法完全弄清楚,如果有人可以将我链接到正确的文章或告诉我什么是我的代码错误,我将不胜感激。将文件中的所有文本更改为小写
答
两个问题:
- 打开与
'r'
读文件。 - 将
lines
更改为file
在您的列表中的理解。
这里的固定代码:
from itertools import chain
from glob import glob
file = open('FortInventory.txt', 'r')
lines = [line.lower() for line in file]
with open('FortInventory.txt', 'w') as out:
out.writelines(sorted(lines))
要开始,你永远不迭代的实际文件。应该是“lines = [line.lower()for line in file]” – Enders
ahhhh duhhhhhh,哑巴我谢谢你! – goldenwest