来自文本文件的小写

时间:2012-12-28 09:23:49

标签: python file-io

我有一个文件,我试图以小写字母转换单词。这就是我到目前为止所拥有的

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

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

5 个答案:

答案 0 :(得分:5)

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

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

会更好......

答案 1 :(得分:5)

而不是

lines = fileinput.lower()

把这个:

line = line.lower()

完整代码:

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

这样可以正常工作。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

file = open('example.txt', 'rt').read().lower()