将CSV文件导入Python以及主题和教师任务Python

将CSV文件导入Python以及主题和教师任务Python,python,csv,Python,Csv,我的任务是创建一个Python文件,该文件从我创建的CSV文件导入数据: python程序必须将CSV列存储在列表中 程序要求教师提供用户必须输入的6个科目 然后,它必须交叉引用导入的CSV并生成教师姓名: 目前,我的代码只能要求一个主题。如果我输入了一个以上的科目,它就不起作用了。有人能帮我写一个代码,询问6个科目,告诉每个老师每个科目的名字吗?谢谢 代码: import csv with open('teachers.csv') as csvfile: readCSV = csv.read

我的任务是创建一个Python文件,该文件从我创建的CSV文件导入数据: python程序必须将CSV列存储在列表中 程序要求教师提供用户必须输入的6个科目 然后,它必须交叉引用导入的CSV并生成教师姓名: 目前,我的代码只能要求一个主题。如果我输入了一个以上的科目,它就不起作用了。有人能帮我写一个代码,询问6个科目,告诉每个老师每个科目的名字吗?谢谢

代码:

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

subjects = []
teachers = []

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

    for row in readCSV:
        subjects.append(row[0])
        teachers.append(row[1])

# ---

# for test
#subjects = ['a', 'b', 'a', 'c']
#teachers = ['X', 'Y', 'Z', 'Q']

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

what_subject = what_subject.split(' ')

if len(what_subject) > 6:
    print("too much subjects")

for one_subject in what_subject:

    # find more then one teacher
    theteacher = []
    subjectdex = 0

    while True:
        # find next - start at "subjectdex"
        try:
            subjectdex = subjects.index(one_subject, subjectdex)
        except:
            break # no more subjects - leave loop

        theteacher.append(teachers[subjectdex])

        subjectdex += 1 # new start for "index"

    theteacher = ', '.join(theteacher)

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

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

使用
For
循环将所有内容重复6次

for x in range(6):
   what_subject = ... 
   subjectdex = ...
   theteacher = ...
   print(...)
如果在一行文本中有主题,则使用
split(“”)

text = input("subjects (separated by one space): ")

subjects = text.split(' ')

if len(subjects) > 6:
   print("too much subjects")

for s in subjects:
   subjectdex = ...
   theteacher = ...
   print(...)

编辑:

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

subjects = []
teachers = []

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

    for row in readCSV:
        subjects.append(row[0])
        teachers.append(row[1])

# ---

# for test
#subjects = ['a', 'b', 'a', 'c']
#teachers = ['X', 'Y', 'Z', 'Q']

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

what_subject = what_subject.split(' ')

if len(what_subject) > 6:
    print("too much subjects")

for one_subject in what_subject:

    # find more then one teacher
    theteacher = []
    subjectdex = 0

    while True:
        # find next - start at "subjectdex"
        try:
            subjectdex = subjects.index(one_subject, subjectdex)
        except:
            break # no more subjects - leave loop

        theteacher.append(teachers[subjectdex])

        subjectdex += 1 # new start for "index"

    theteacher = ', '.join(theteacher)

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

下面是我使用类似任务创建的代码。我用游戏代替主题。只需更改字符串

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

使用
for
循环来询问更多主题。对不起,它没有显示任何内容,我无法理解。您说您的程序只显示一个主题,但需要6个-因此使用
for
循环来重复相同的命令6次。或者使用
拆分(“”)
然后您可以在一行中回答6个主题“主题1主题2主题3主题4主题5主题6”没问题,伙计:)