Python中,字符串在for循环之后被多次打印出来,尽管我有一个中断

Python中,字符串在for循环之后被多次打印出来,尽管我有一个中断,python,string,loops,csv,for-loop,Python,String,Loops,Csv,For Loop,我正试图让我的程序打印,不幸的是我们找不到。。。如果用户在我的文本文件中输入一个没有匹配关键字的字符串,则返回一次字符串。但是,它会持续打印与用户输入字符串中的字数相同的次数。。。我相信这是因为我在代码的前面使用了.split将用户输入转换成数组,但我找不到修复它的方法。我尝试过使用“next”和“any”,但没有成功。有什么建议吗?如果希望外部for循环停止检查,那么您也应该退出外部for循环: import csv def Start(): query = input("\nWh

我正试图让我的程序打印,不幸的是我们找不到。。。如果用户在我的文本文件中输入一个没有匹配关键字的字符串,则返回一次字符串。但是,它会持续打印与用户输入字符串中的字数相同的次数。。。我相信这是因为我在代码的前面使用了.split将用户输入转换成数组,但我找不到修复它的方法。我尝试过使用“next”和“any”,但没有成功。有什么建议吗?

如果希望外部for循环停止检查,那么您也应该退出外部for循环:

import csv 

def Start():
    query = input("\nWhat is wrong with your mobile device? ").upper().split() 
    keyword = len(query) 

    for i in range(keyword):
        filename = ("Advice.csv")
        with open(filename) as csvfile:
            reader = csv.DictReader(csvfile) 
            for row in reader:
                if query[i] == row['KEYWORDS']:
                    print(row['ADVICE']) 
Start()
我还建议更改循环顺序,因为这样可以打开文件一次,处理速度更快:

for i in range(keyword):
    get_out = False
    filename = ("Advice.csv")
    with open(filename) as csvfile: #Opens the text file 'Advice.csv' as a csvfile
        reader = csv.DictReader(csvfile) #Reads the file.
        for row in reader:
            if query[i] == row['KEYWORDS']:
                print(row['ADVICE']) #Prints advice linked to keywords.
            else:
                print("Unfortunately we could not find any advice in our database, we recommend calling your supplier or rephrasing your response.")
                get_out = True
                break
    if get_out:
        break

我认为这会很有帮助。

您只需稍微修改一下代码结构,问题就会迎刃而解:

filename = ("Advice.csv")
with open(filename) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
    get_out = False
        for i in range(keyword):
            if query[i] == row['KEYWORDS']:
                print(row['ADVICE']) #Prints advice linked to keywords.
            else:
                print("Unfortunately we could not find any advice in our database, we recommend calling your supplier or rephrasing your response.")
                get_out = True
                break
if get_out:
    break
我已对您的代码进行了以下修改:

创建跟踪匹配数的var结果。我们在最后两行中使用这个变量来确定是否打印字符串。当然,您可以对此使用布尔值,并在找到匹配项时将其设置为true。然而,我选择计算匹配项,因为您可能可以在某个地方使用该信息。 将I/O移到循环外部。这不是你的问题的一部分,但是我包括了它,因为当你只读取一次文件时,无论用户搜索多少个关键字,它都会大大提高性能。 此外,根据文件的大小,切换循环的顺序可能会对您有所帮助。外循环是读卡器,内循环是查询,这会减少迭代次数

更好的是,您可以完全像这样抛弃双循环:

#I/O
filename = ("Advice.csv")
with open(filename) as csvfile:
    reader = csv.DictReader(csvfile)

#Initial value of results is 0
results = 0 

#Loop through items
for i in range(keyword):
    for row in reader:
        if query[i] == row['KEYWORDS']:
            print(row['ADVICE']) 
            results += 1 #Increment

if not results:  #This is shorthand for if results == 0
    print("Unfortunately we could not find any advice in our database, we recommend calling your supplier or rephrasing your response.")

当然,这是一个一般性的建议。由于您没有提供足够的代码,因此我无法确定它是否会起作用。不过,看看您是否可以在程序中实现类似的功能。

谢谢您的帮助!我尝试了你描述的两种方法,它们都很有效。谢谢你的帮助!
if row["KEYWORDS] in query:
    print(row["ADVICE"])
else:
     print("Unfortunately we could not find any advice in our database, we recommend calling your supplier or rephrasing your response.")