将CSV文件导入到Python和主题和教师任务Python [英] Importing CSV files to Python and subject and teacher task Python

查看:111
本文介绍了将CSV文件导入到Python和主题和教师任务Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个Python文件,从我创建的CSV文件中导入数据:
然后,python程序必须将CSV列存储在列表中
程序要求教师6个主题用户必须输入
然后必须交叉引用导入的CSV并生成教师的名称:
此时,我的代码只能请求一个主题。如果我输入超过1个主题,它不工作。有人可以帮我写一个代码,要求6个科目,并告诉每个教师的每个主题的名字?感谢。

My task is to create a Python file that imports data from the CSV file I have created: The python program then must store the CSV columns in a list The program asks the teacher for 6 subjects which the user must enter It must then cross reference the imported CSV and produce the name of the teacher: At the moment, my code can only ask for one subject. If i enter more than 1 subject, it doesnt work. Can someone help me to write a code which asks the 6 subjects and tell each teacher name of each subject? Thanks.

CODE:

import csv

with open('teachers.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')

subjects = []
teachers = []


for row in readCSV:
    subject = row[0]
    teacher = row[1]


    subjects.append(subject)
    teachers.append(teacher)


what_subject = input("Which subjects did you have today at school? ")
subjectdex = subjects.index(what_subject)

theteacher = teachers[subjectdex]

print("The teachers of", what_subject, "are", theteacher)

出于个人原因,我无法上传CSV文件,因为它有个人名字。

For personal reasons, I can't upload the CSV file since it has personal names.

推荐答案

这是一个我用类似的任务创建的代码。而不是主题,我使用游戏。只需更改字符串。

Here is a code which I created with a similar task. Instead of subjects, I used games. Simply change the strings.

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

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

这篇关于将CSV文件导入到Python和主题和教师任务Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆