[文件处理I / O读取文件

问题描述 投票:1回答:1

我将非常感谢您的帮助。(文本文件中的记录:巴西\ n12 \ nethopia \ n23 \ nmada \ n10 \ n)。例如,当我键入“巴西”或开头部分(如b或br或bra等)时,输出为:巴西12我希望仅当全名被写入而不是其开头部分时,输出才是正确的。我尝试了==代替(....如果在行中搜索:。因此,在此先感谢您的帮助

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()

python file search operator-keyword
1个回答
0
投票

与您的情况下的'\ n连接search

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()
© www.soinside.com 2019 - 2024. All rights reserved.