当前代码......
import csv
import itertools
students = {}
def studentGradeCSV():
with open('/Users/home/Downloads/gradebook-export.csv') as csvfile:
reader = csv.DictReader(csvfile)
global students
students = {}
for row in reader:
name = row['Last Name'] + ', ' + row['First Name']
grade = row['Overall']
new_grade = []
for g in grade:
try:
v = int(float(g))
new_grade.append(v)
except ValueError:
new_grade.append(g)
for n, gr in itertools.product(name, new_grade):
students[n] = gr
'成绩'指数示例 - 'N / A','92 .13','100','87 .2','91','0'
'grade'变量是字符串。我想将它们转换为整数,然后将它们作为'name'变量中值的键值。我以前能够将'grade'int键转换为名称值但是它们被错误地分配,因为字典没有以正确的顺序抓取它们。
不确定从哪里开始?
答案 0 :(得分:1)
不确定为什么使用for
循环来表示单个值:
name = row['Last Name'] + ', ' + row['First Name']
grade = row['Overall']
try:
grade = int(float(grade))
except ValueError:
pass
students[name] = grade
您的for
循环和itertools.product
将字符串视为一系列字符,例如
for g in 'N/A':
将产生new_grade = ['N', '/', 'A']
。
然后:
itertools.product("Name", ['N', '/', 'A']
生成序列('N', 'N'), ('N', '/'), ('N', 'A'), ('a', 'N'), ...
,而不是您正在查找的内容。