Python中的For循环和从fi读取输入

2024-05-13 22:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想弄明白为什么我得到else语句的多个返回结果。 我正试图从一个文件中读取。用户将输入一些内容 如果这个东西和文件中的一个单词或一个句子/数字匹配 把那行打印出来。如果在文件中找到这个单词,它就可以正常工作。文件 打印文档中匹配单词的内容。 但我的问题是关于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语句)的输出,打印出文档中找到的正确行:

NEW: danny yes its in here: 10101 does he know does he know. danny

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

NEW: dude

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

Word: dude not in here

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

NEW: danny

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here Word: danny not in here

Word: danny not in here

Word: danny not in here

yes its in here: 10101 does he know does he know. danny

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

Word: danny not in here

任何方向正确的帮助都会很好。你知道吗

谢谢你, 丹尼


Tags: 文件innewherelinenot语句else
3条回答

另一种替代使用标志的方法是将此逻辑包含在函数中,如果找到单词,则返回行;如果找不到单词,则返回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()显式关闭它,这意味着在某些情况下文件将无法正确关闭。(这在这里可能不是问题,但在将来可能是)

另一个优点是,将代码转换为函数可以更容易地使用不同的搜索关键字多次运行查询。你知道吗

设置一个标志并在检查完文件后打印出来。你知道吗

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

你必须像旗子一样使用它来显示你是否匹配这个词。例如:

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

我认为这应该管用。你知道吗

相关问题 更多 >