如何在Python中调试“AttributeError:'tuple'对象没有属性'readline'”?

时间:2017-09-16 08:51:34

标签: python

AttributeError: 'tuple' object has no attribute 'readline'

引诱我Metadata file '.dll' could not be found

1 个答案:

答案 0 :(得分:0)

  1. 您的错误位于coffee_file=('c:/test/coffee.txt','r')行。你应该写coffee_file=open('c:/test/coffee.txt','r')。我使用with语句替换了上下文管理器。
  2. 您的while循环是无限的,所以我使用了for循环。
  3. 您正在比较字符串"搜索"使用变量" descr",这是文件的第一行。
  4. 假设您要在文件中找到提升的字符串 - 此代码应该有效:

    def main():
        found = False
        search = input("Enter a description to search :- ").rstrip()
    
        with open('c:/test/coffee.txt') as coffee_file:
            for line in coffee_file:
                qty = line.rstrip()
                if search == qty:
                    print("Description :- ", search)
                    print("Quantity :- ", qty)
                    found = True
    
        if not found:
            print("Description :- ", search)
            print("Quantity :- not found")
    
    main()
    

    P.S。请在下次提供输入数据(在本例中为c:/test/coffee.txt)样本和预期输出。