【问题标题】:For loop in Python and reading input from filePython中的for循环并从文件中读取输入
【发布时间】:2016-02-22 01:18:04
【问题描述】:

我试图弄清楚为什么我会为 else 语句获得多个返回结果。 我正在尝试从文件中读取。用户将输入一些东西 如果该内容与文件中的单词或句子/数字匹配 打印出该行。如果在文件 inside 中找到该单词,则它可以正常工作。文件 打印出文档中匹配单词的内容。 但我的问题与 else 语句有关。如果该单词不在文档中, 它用 else 的 print 语句打印出每一行。 我明白我把它放在 for 循环中,它会迭代所有实例 文件中的行数。我的最终目标只是打印出 else 语句的一个实例,而不是在每一行上。

file=open('new_file.txt','r')
new_user=input(str('NEW: '))
for line in file:
    line=str(line)
    if new_user in line:
        print('yes its in here: ',line)
    else:
        print('Word: ',new_user,' not in here')
file.close()

不带(else 语句)输出,打印出文档中找到的正确行:

新:丹尼 是的,它在这里:10101 他知道他知道吗。丹尼

Output WITH (else):打印出所有不匹配的行。我只想打印出 else 块中的语句的一个实例:

新:老兄

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

字:老兄不在这里

在文档中输入匹配名称的 else 语句输出:

新:丹尼

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在此处 单词:danny 不在此处

单词:danny 不在这里

单词:danny 不在这里

是的,它在这里:10101 他知道他知道吗。丹尼

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

单词:danny 不在这里

对正确方向的任何帮助都会很棒。

谢谢, 丹尼

【问题讨论】:

    标签: python python-3.x if-statement for-loop


    【解决方案1】:

    你必须使用像标志一样的东西,如果你匹配了这个词,它会告诉你。例如:

    file=open('new_file.txt','r')
    new_user=input(str('NEW: '))
    notFound = 1 # The flag that shows if the user was found.
    for line in file:
        line=str(line)
        if new_user in line:
            print('yes its in here: ',line)
            notFound = 0 # Now is false.
    if notFound: # Check if the user wasn't found.
        print('Word: ',new_user,' not in here')
    file.close()
    

    我认为应该可以。

    【讨论】:

      【解决方案2】:

      替代使用标志的另一种选择是将此逻辑包含在函数中,如果找到该单词则返回该行,如果从未找到则返回None

      def find_word (file, word):
          with open(file) as infile:
              for line in infile:
                  if word in line:
                      return line
          return None
      
      new_user=input(str('NEW: '))
      ln = find_word('new_file.txt', new_user)
      
      if (ln):
          print('yes its in here: ',ln)
      else:
          print('Word: ',new_user,' not in here')
      

      解释:

      通过在找到匹配项后立即返回函数内部,您可以确保它只会返回您要查找的关键字的第一个实例。

      在切换到使用with open('filename') as ... 语法打开文件时,您获得的优势是,如果在读取文件时抛出异常,文件仍将关闭。它还会在返回之前关闭文件。您当前首先使用file=open(...) 打开文件然后在最后使用file.close() 显式关闭它的方法意味着在某些情况下文件将无法正确关闭。 (这里可能不是问题,但将来可能会出现)

      另一个优点是将代码转换为函数可以更轻松地使用不同的搜索关键字多次运行查询。

      【讨论】:

        【解决方案3】:

        设置一个标志并在您检查完文件后将其打印出来。

        file=open('new_file.txt','r')
        new_user=input(str('NEW: '))
        flag = False
        for line in file:
            line=str(line)
            if new_user in line:
                print('yes its in here: ',line)
                flag = True
        if not flag:
            print('Word: ',new_user,' not in here')
        file.close()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-16
          • 2017-10-12
          • 2013-08-28
          • 2022-11-18
          • 1970-01-01
          • 2011-04-21
          • 1970-01-01
          相关资源
          最近更新 更多