Python If语句不是';使用字典或列表时不工作

Python If语句不是';使用字典或列表时不工作,python,list,csv,dictionary,Python,List,Csv,Dictionary,我有一个表格,上面有学生ID和学生读过的幻灯片。 现在我需要打印出那些读了200多张幻灯片的学生,并计算其中有多少张。我尝试将csv导入列表和字典。 我发现不可能像我在C语言中学习的那样使用if和while循环 #1 import csv f = open('student_example.csv','r') for row in csv.reader(f): with open('student_example.csv', mode='w') as outfile:

我有一个表格,上面有学生ID和学生读过的幻灯片。 现在我需要打印出那些读了200多张幻灯片的学生,并计算其中有多少张。我尝试将csv导入列表和字典。 我发现不可能像我在C语言中学习的那样使用if和while循环

#1
import csv
f = open('student_example.csv','r')


for row in csv.reader(f):
     with open('student_example.csv', mode='w') as outfile:
         writer = csv.writer(outfile)
         [k,v] = row
         f[k] = v
         k = 0
         print (f[2])
         while k < 71:    #There are 71 students
            k += 1
            if f[k] >= 200:
                print (f[k])
#2
import csv

with open('student_example.csv', mode='r') as infile:
   reader = csv.reader(infile)

   with open('student_example_new.csv', mode='w') as outfile:
       writer = csv.writer(outfile)

       mydict = {rows[0]:rows[1] for rows in reader}
       print(len(mydict))
       k = 200
       for rows[1] > k in reader:
            print(rows[1])
#1
导入csv
f=打开('student_example.csv','r')
对于csv.reader(f)中的行:
以open('student_example.csv',mode='w')作为输出文件:
writer=csv.writer(输出文件)
[k,v]=行
f[k]=v
k=0
打印(f[2])
而k<71:#有71名学生
k+=1
如果f[k]>=200:
打印(f[k])
#2
导入csv
以open('student_example.csv',mode='r')作为填充:
reader=csv.reader(infle)
以open('student\u example\u new.csv',mode='w')作为输出文件:
writer=csv.writer(输出文件)
mydict={rows[0]:reader}中的行的行[1]
打印(len(mydict))
k=200
对于读取器中的[1]>k行:
打印(第[1]行)
样本输出应为: 通过考试的学生有13名:口述键(['B046060035','B064020028','B064020052','B064030002','B06504041',',
‘B066060019’、‘B066090002’、‘B073040038’、‘B074020003’、‘B074020024’、‘B074020043’、‘B074020053’、‘B074020055’))

第一次尝试时,您可以这样做。 您希望打开outfile,然后遍历学生。 检查每个学生的幻灯片数量,如果>200,则检查他们读写到outfile的幻灯片数量

import csv
f = open('student_example.csv','r')

with open('student_example.csv', mode='w') as outfile:
    writer = csv.writer(outfile)
    for row in csv.reader(f):
        [student_id, slides] = row
        if slides > 200:
            writer.writerow(student_id)
            print (student_id)