简体   繁体   English

AttributeError: '_io.TextIOWrapper' 对象没有 txt 文件的属性 'lower'

[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'lower' for txt file

Here is my code (A pig Latin translator from a text file):这是我的代码(来自文本文件的猪拉丁语翻译器):

f = open('Assignment_4.txt', 'r+')

for line in f:
    print(line)

def pigLatin():
    var = 'ay'
    wordL = f.lower()
    firstLetter = wordL[0]
    pigLatin = wordL + firstLetter + var
    pigLatin = pigLatin[1:]
    print(pigLatin)

It works for defined strings but won't for the file.它适用于定义的字符串,但不适用于文件。 Help is appreciated!帮助表示赞赏!

Points:积分:

  • lower() works with string. lower()适用于字符串。 You are trying to use with file handle f .您正在尝试与文件句柄f一起使用。 That's why you are getting this error.这就是您收到此错误的原因。
  • Also, after reading file line by line, you should call pigLatin() for each line as pigLatin(line) .此外,在逐行读取文件后,您应该为每一行调用pigLatin()pigLatin(line) So, now , pigLatin() function expects one argument.所以,现在, pigLatin() 函数需要一个参数。
  • Also, close the file at the end as f.close() .另外,在最后关闭文件f.close() It will be if you use with statement to open file.如果您使用with语句打开文件,它将是。

Code with comments inline:带有内联注释的代码:

def pigLatin(stuff_to_be_changed):
    var = 'ay'
    wordL = stuff_to_be_changed.lower()
    firstLetter = wordL[0]
    pigLatin = wordL + firstLetter + var
    pigLatin = pigLatin[1:]
    print(pigLatin)


#For string

string =  "I am to change"
#Call function
pigLatin(string)

f = open('Assignment_4.txt', 'r+')

#For file
for line in f:
    print(line)
    #Call function
    pigLatin(line)

#Close the file
f.close()

The error is quite right - file objects don't have a lower() method - before you can use your function you need to read a line of text from your file and split it into separate words.错误是正确的 - 文件对象没有lower()方法 - 在您可以使用您的函数之前,您需要从文件中read一行文本并将其split为单独的单词。 (Note that it is never a good idea to use the same name for a variable and a method as it can cause confusion.) (请注意,对变量和方法使用相同的名称从来都不是一个好主意,因为这会导致混淆。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'_ io.TextIOWrapper'对象没有属性'lower' - AttributeError: '_io.TextIOWrapper' object has no attribute 'lower' AttributeError: '_io.TextIOWrapper' 对象没有属性 'сlose'。 试图关闭打开的 .txt 文件 - AttributeError: '_io.TextIOWrapper' object has no attribute 'сlose'. Trying to close opened .txt file Python-错误-AttributeError:“ _ io.TextIOWrapper”对象没有属性“插入” - Python - Error - AttributeError: '_io.TextIOWrapper' object has no attribute 'insert' Python问题:AttributeError:'_ io.TextIOWrapper'对象没有属性'split' - Python Issue: AttributeError: '_io.TextIOWrapper' object has no attribute 'split' AttributeError:'_io.TextIOWrapper'对象没有属性'show' - AttributeError: '_io.TextIOWrapper' object has no attribute 'show' 错误:AttributeError:'_io.TextIOWrapper'对象没有属性'split' - ERROR: AttributeError: '_io.TextIOWrapper' object has no attribute 'split' AttributeError:'_io.TextIOWrapper'对象没有属性'reader' - AttributeError: '_io.TextIOWrapper' object has no attribute 'reader' AttributeError:'_io.TextIOWrapper'对象没有属性'append' - AttributeError: '_io.TextIOWrapper' object has no attribute 'append' 什么是“AttributeError:'_ it.TextIOWrapper'对象在python中没有属性'replace'”? - What is a “ AttributeError: '_io.TextIOWrapper' object has no attribute 'replace' ” in python? Python-AttributeError:“ _ io.TextIOWrapper”对象没有属性“ append” - Python - AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM