我有一个文件,正在尝试将单词转换为小写。这是我到目前为止所拥有的
with open('example.txt', 'r') as fileinput:
for line in fileinput:
lines = fileinput.lower()
AttributeError: 'file' object has no attribute 'lower'
您正在小写文件对象,而不是行。
with open('example.txt', 'r') as fileinput:
for line in fileinput:
line = line.lower()
会更好..
代替
lines = fileinput.lower()
把这个:
line = line.lower()
完整代码:
with open('example.txt', 'r') as fileinput:
for line in fileinput:
line = line.lower()
这是用于此目的的一行代码
file = open('example.txt', 'rt').read().lower()
with open('example.txt', 'r') as fileinput:
for line in fileinput:
line = line.lower()
#do something
with open('example.txt', 'r') as fileinput:
for line in fileinput:
line = line.rstrip().upper()
print line