4

我有一个文件,正在尝试将单词转换为小写。这是我到目前为止所拥有的

with open('example.txt', 'r') as fileinput:
   for line in fileinput:
       lines = fileinput.lower()

AttributeError: 'file' object has no attribute 'lower'
4

5 回答 5

7

您正在小写文件对象,而不是行。

with open('example.txt', 'r') as fileinput:
   for line in fileinput:
       line = line.lower()

会更好..

于 2012-12-28T09:25:21.447 回答
6

代替

lines = fileinput.lower()

把这个:

line = line.lower()

完整代码:

with open('example.txt', 'r') as fileinput:
    for line in fileinput:
        line = line.lower()

这将毫无错误地工作。

于 2012-12-28T09:35:55.807 回答
3

这是用于此目的的一行代码

file = open('example.txt', 'rt').read().lower()
于 2019-01-29T09:18:14.870 回答
1
with open('example.txt', 'r') as fileinput:
   for line in fileinput:
       line = line.lower()
       #do something
于 2012-12-28T09:25:55.683 回答
1
with open('example.txt', 'r') as fileinput:
   for line in fileinput:
       line = line.rstrip().upper()
       print line
于 2016-05-28T09:58:35.950 回答