文件处理 I/O 读取文件

file handling I/O reading file

如有帮助,将不胜感激。(文本文件中的记录:brazil\n12\nethopia\n23\nmada\n10\n)。例如,当我输入 "brazil" 或开头的一部分(b 或 br 或 bra 等等)时,输出是: 巴西 12 我希望输出只有在写全名而不是它的开头部分时才是正确的。我尝试用 == 代替 in(....如果在线搜索:....)但它不起作用。在此先感谢您的帮助

def main():

    def data():
        dfile= open("coffeedata.txt","a")
        user = input('Insert a record: ')
        while user == "y":
            desr = input("name of coffe: ")
            dfile.write(desr+"\n")
            qty = float(input("insert the quantity: "))
            dfile.write(str(qty)+"\n")
            user = input('do you have new record: y or n: ')
        else:
            print("no data entry")
    data()


    def dreading():
        with open("coffeedata.txt","r") as dfile:
            for line in dfile:
                print (line.rstrip("\n"))
    dreading()

    def searching():
        found= False
        search = input("name of coffee:")  
        with open("coffeedata.txt", "r") as dfile:
            print(dfile)
            for line in dfile:
                if search in line:  
                    print(line.rstrip("\n"))
                    print(type(line))
                    qty=float(dfile.readline())
                    print(qty)
                    print(type(qty))
                    found=True
            if not found:
                print("the coffee is not in the record")

    searching()

main()

search'\n 连接起来

if search + "\n" in line:

>

def main():
  def data():
      dfile= open("coffeedata.txt","a")
      user = input('Insert a record: ')
      while user == "y":
          desr = input("name of coffe: ")
          dfile.write(desr+"\n")
          qty = float(input("insert the quantity: "))
          dfile.write(str(qty)+"\n")
          user = input('do you have new record: y or n: ')
      else:
          print("no data entry")
  data()


  def dreading():
      with open("coffeedata.txt","r") as dfile:
          for line in dfile:
              print (line.rstrip("\n"))
  dreading()

  def searching():
      found= False
      search = input("name of coffee:")  
      with open("coffeedata.txt", "r") as dfile:
          print(dfile)
          for line in dfile:
              if search + "\n" in line:  
                  print(line.rstrip("\n"))
                  print(type(line))
                  qty=float(dfile.readline())
                  print(qty)
                  print(type(qty))
                  found=True
          if not found:
              print("the coffee is not in the record")

  searching()

main()