Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要使用输入将excel文件中同一行中的值相乘_Python_Python 3.x - Fatal编程技术网

Python 需要使用输入将excel文件中同一行中的值相乘

Python 需要使用输入将excel文件中同一行中的值相乘,python,python-3.x,Python,Python 3.x,我需要能够在input()中键入动物的名称,以便显示价格X数量。但是,此数据表位于excel文件中,因此我不知道如何从input()引用excel文件中的“ANIMAL”列,也不知道如何包括价格和数量 import csv file_reader = csv.DictReader(open('FILENAME.CSV','r')) for row in file_reader: print(row) animal_input=input("What kind of animal?

我需要能够在
input()
中键入动物的名称,以便显示价格X数量。但是,此数据表位于excel文件中,因此我不知道如何从
input()
引用excel文件中的“ANIMAL”列,也不知道如何包括价格和数量

import csv

file_reader = csv.DictReader(open('FILENAME.CSV','r'))

for row in file_reader:

    print(row)

animal_input=input("What kind of animal?")

print("To buy all those animals costs:"PRICE*QUANTITY)

或许可以使用口述,并在阅读时计算结果

ANIMAL         PRICE   QUANTITY
ANTEATER         5         4
BEAR             3         4
CAT              3         4
DOG              2         3
ECHIDNA          2         2
演示:


您可以将整个csv作为数据帧读取,然后使用查找来获取所需的值。
import csv
import StringIO

csv_pretend_file = """ANIMAL,PRICE,QUANTITY
ANTEATER,5,4
BEAR,3,4
CAT,3,4
DOG,2,3
ECHIDNA,2,2"""

animals = {}

for row in csv.DictReader(StringIO(csv_pretend_file)):
    animals[row['ANIMAL']] = int(row['PRICE']) * int(row['QUANTITY'])


animal_input=input("What kind of animal?")
print("To buy all those animals costs: {}".format(animals[animal_input]))
What kind of animal?ANTEATER


To buy all those animals costs: 20