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

标签 python csv

我的任务是使用 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)

P.S:我使用了 StackOverflow 中另一个主题提出的另一个问题中的代码。

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

当我按 ENTER 时,会出现以下内容:

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

关于python - 用户在输入中输入一个句子,Python 挑选出关键字并从 CSV 文件中获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35148321/

相关文章:

Python 3 : Output R, G,B 到 CSV - 索引错误:图像索引超出范围

python - Pandas 用 df.apply 结果替换原始列空值

node.js - 如何将 ip 地址与 maximind 数据匹配?

python - Pandas 基于 key 格式化 csv 数据

ruby - 如何将散列输出到 CSV 行

c++ - OpenCV 图像处理——C++ vs C vs Python

python - 如何在 Django 聚合查询的 GROUP BY 子句中正确包含列?

python - Maya python 连接选择的属性

python - 写入 CSV 文件时如何追加到新行

regex - 使用正则表达式从 CSV 中删除多余的引号字符