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)我需要能够在input()中输入动物的名字,这样就会出现价格X数量。但是,这个数据表在excel文件中,所以我不知道如何从input()引用到Excel文件中的“动物”列,也不知道如何包含价格和数量。
ANIMAL PRICE QUANTITY
ANTEATER 5 4
BEAR 3 4
CAT 3 4
DOG 2 3
ECHIDNA 2 2发布于 2017-09-13 13:20:40
也许可以使用字典,然后计算阅读的结果。
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: 20https://stackoverflow.com/questions/46189034
复制相似问题