用户在输入中输入一个句子,Python从CSV文件中选择关键字并获取它

用户在输入中输入一个句子,Python从CSV文件中选择关键字并获取它,python,csv,Python,Csv,我的任务是用CSV文件创建代码。我必须创建一个代码,在我运行它之后,用户必须在一个句子中输入答案,然后Python必须从这个句子中选择关键字,并从CSV文件中交叉引用,然后给用户一个答案。 我创建了一个代码,该代码要求: 您想知道哪些游戏的价格? 用户必须写一个答案,可能会说: 我想知道的游戏价格是:FarCry4 GTA 5 然后Python必须挑出单词FarCry4和GTA5。导入CSV文件后,它必须告诉游戏的价格,但我不能这样做,因为Python会选择Python中的每个单词并告诉价格。请

我的任务是用CSV文件创建代码。我必须创建一个代码,在我运行它之后,用户必须在一个句子中输入答案,然后Python必须从这个句子中选择关键字,并从CSV文件中交叉引用,然后给用户一个答案。 我创建了一个代码,该代码要求:

您想知道哪些游戏的价格?

用户必须写一个答案,可能会说:

我想知道的游戏价格是:FarCry4 GTA 5

然后Python必须挑出单词FarCry4GTA5。导入CSV文件后,它必须告诉游戏的价格,但我不能这样做,因为Python会选择Python中的每个单词并告诉价格。请帮忙

代码:

import csv

games = []#subjects
howmuchitcosts = []#teachers

with open('gamesandprices.csv') as csvfile:
    readCSV = csv.reader(csvfile)

    for row in readCSV:
        games.append(row[0])
        howmuchitcosts.append(row[1])


what_game = input("Which game(s) would you like to find out the price of?: ")

what_game = what_game.split(' ')

if len(what_game) > 6:
    print("Too many games. Only type in 6 games.")

for one_game in what_game:

    theprice = []
    gamedex = 0

    while True:

        try:
            gamedex = games.index(one_game, gamedex)
        except:
            break 

        theprice.append(howmuchitcosts[gamedex])

        gamedex += 1 

    theprice = ', '.join(theprice)

    print("The price of", one_game, "is", theprice)
Too many games. Only type in 6 games.
The price of I is 
The price of want is 
The price of to is 
The price of know is 
The price of the is 
The price of price is 
The price of of is 
The price of GTA5 is £30.99
The price of and is 
The price of FarCry4 is £40.99
注:我使用了StackOverflow中另一个主题中的另一个问题的代码

当我输入以下句子时: 我想知道GTA5和FarCry4的价格

当我按ENTER键时,会显示:

import csv

games = []#subjects
howmuchitcosts = []#teachers

with open('gamesandprices.csv') as csvfile:
    readCSV = csv.reader(csvfile)

    for row in readCSV:
        games.append(row[0])
        howmuchitcosts.append(row[1])


what_game = input("Which game(s) would you like to find out the price of?: ")

what_game = what_game.split(' ')

if len(what_game) > 6:
    print("Too many games. Only type in 6 games.")

for one_game in what_game:

    theprice = []
    gamedex = 0

    while True:

        try:
            gamedex = games.index(one_game, gamedex)
        except:
            break 

        theprice.append(howmuchitcosts[gamedex])

        gamedex += 1 

    theprice = ', '.join(theprice)

    print("The price of", one_game, "is", theprice)
Too many games. Only type in 6 games.
The price of I is 
The price of want is 
The price of to is 
The price of know is 
The price of the is 
The price of price is 
The price of of is 
The price of GTA5 is £30.99
The price of and is 
The price of FarCry4 is £40.99
但是我希望python只选择GTA5和FarCry4,并告诉这些游戏的价格,而不是整个句子


这来自另一个主题,因为他正在处理相同的代码:)

更改读取csv的代码以生成词典,然后只需从输入中查找单词。如果字典里有这个词,那就意味着你有这个游戏的价格;因此,仅打印输出,然后:

import csv

price_list = {} # this is an empty dictionary

with open('gamesandprices.csv') as csvfile:
    readCSV = csv.reader(csvfile)
    for row in readCSV:
        price_list[row[0]] = row[1]

what_game = input("Which game(s) would you like to find out the price of?: ")
what_game = what_game.split(' ')

# Now what we want to do, is for each word in the input, check if it
# exists in our dictionary. If it does exist, it must be the name of
# a game, so we need to know its price.

# We also want to make sure we limit the number of games in one
# request to 6 games. To find out how many games the person entered,
# lets filter all the words and collect those words (and prices)
# that are games in a list.

# The long way of writing this is:
# results = [] - this is an empty list
# for word in what_game:
#    if word in price_list: 
#        results.append(word, price_list[word])

results = [(game, price_list[game]) for game in what_game if game in price_list]

if len(results) > 6:
    print('Please ask prices for a maximum of 6 games')
else:
    for game, price in results:
        print('The price of {} is {}'.format(game, price))

只需列出游戏列表,并将输入与名称匹配即可。应该可以。您的主要问题是用户输入中的派生。例如,
FarCry5
FarCry5
甚至
FarCry5
都不一样。您将需要某种类型的误差和相似性检查。谷歌“python字符串相似性比较”或谷歌
Levenshtein distance
在游戏中添加
if one\u游戏:
就在
之后,对于what\u游戏中的one\u游戏:
为我修复了它。我认为代码不应该有一个完整的句子作为输入。我意识到最后两行的缩进改变了很多东西。如果在while循环中包含
theprice='、'.join(theprice)
print(“一个游戏的价格”是“theprice”)
,则结果中只有两个游戏,如果没有,则有当前结果。然而,“太多的游戏”仍然会存在,你需要用游戏列表过滤分割。Morb和Torxed,你能给我看一下对你们有用的代码吗?是的,它必须有一个完整的句子作为输入,然后它必须从代码中选择关键字。它可以工作,但仍然有太多的游戏。只能输入6个游戏。句子。对不起,cmd对于复制/粘贴来说是垃圾。是的,但现在它实际上做到了锡上所说的。伯汗,谢谢!!!!如果你有时间,你能解释一下代码是如何工作的吗?我想知道代码是如何工作的,字典和格式(game,pric))是如何工作的。有标记的答案是正确的。