Python坚持按顺序将字典写入文件
我对下面这个问题有疑问;我的输出应该如下所示 输出:Python坚持按顺序将字典写入文件,python,python-3.x,Python,Python 3.x,我对下面这个问题有疑问;我的输出应该如下所示 输出: ID Exam1 Exam2 Homework Attendance Project1 Project2 Class Recap Final Grade Potential Grade 32165487 10 70 50 40 100 80 50 100 D 21321853 52 95 72 56 95
ID Exam1 Exam2 Homework Attendance Project1 Project2 Class Recap Final Grade Potential Grade
32165487 10 70 50 40 100 80 50 100 D
21321853 52 95 72 56 95 32 56 100 C+
41861235 95 12 47 32 68 92 35 100 D
84534853 58 38 84 84 89 68 74 100 C
代码:
这是上面代码的输出。如何让它打印所有行而不是一行
project1 homework attendance id exam2 exam1 project2 classrecap
55 46 4 47718233 46 81 14 53
为所有学生评分(评分循环应在学生内部)
为所有学生评分(评分循环应在学生内部)
真的吗?我得到的结果是:
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
将您的代码放入,它会向您显示许多警告中的问题:
您写入文件的唯一位置是第14行和第17行,这两行是学生成绩的循环:
和学生.values()中val的,但您没有在任何地方定义学生
您确实在上一个循环中将其用作循环变量,因此students
具有上一个循环的结束值
要解决这个问题,您需要在成绩册上循环一次,一次得到一个学生,然后对于每个条目,您需要循环学生的数据,以处理文件的行
你的标题“有序”是什么意思?不清楚您是指列还是行,或者两者都应该排序,或者顺序是什么。但是,当您可以使用有序数据结构(元组、列表)时,却使用无序数据结构(字典),这会让您的生活变得更轻松,这让您变得相当愚蠢
(你想要的输出中那些数据中不存在的列呢,比如潜在等级?或者你想要的标题与数据的大小写/间距不同,Python区分大小写,这很烦人?或者csv
模块的存在方式可以为你做很多这方面的工作)
将数据传递到名为成绩册的generateReport
-使用该名称。当你使用d
时,你正在从函数中伸出来,进入父范围,这是一个复杂而棘手的问题,这意味着成绩册
永远不会被使用,这对任何阅读它的人来说都是混乱和无益的
使用有用的名称—student\u id
来迭代学生id,而不是stu
<代码>学生
单数,而不是学生
。任何东西,而不是任何东西
()
关闭outfile以调用close方法def generateReport(gradebook):
outfile=open('gradebook.txt','w')
# all the columns, in the order they need to show up.
columns = ('ID', 'Exam1', 'Exam2', 'Homework', 'Attendance',
'Project1', 'Project2', 'Class Recap',
'Final Grade', 'Potential Grade')
# join the columns into a tab-separated line with a newline, and write it once
header_line = '\t'.join(columns) + '\n'
outfile.write(header_line)
# sort the student IDs into (numeric) order:
sorted_student_ids = sorted(gradebook.keys(), key=int)
# loop over each student ID
for student_id in sorted_student_ids:
# for each student, get their data
student_data = gradebook[student_id]
# build up a line of output for this students, using the columns
student_row = []
for column in columns:
# columns don't match the dictionary keys :|
# remove spaces and make them lowercase, so they do
# also handle where there is no data for a column and get '' instead
# and convert to strings for writing to the file
column = column.replace(' ', '').lower()
value = str(student_data.get(column, ''))
student_row.append(value)
# now we have the data for this student, in the desired column order
student_line = '\t'.join(student_row) + '\n'
outfile.write(student_line)
outfile.close()
d={'37340752': {'exam1': '50', 'project1': '40', 'classrecap': '39', 'homework': '62', 'exam2': '3', 'attendance': '17', 'project2': '86', 'id': '37340752'},
'95255664': {'exam1': '76', 'project1': '60', 'classrecap': '39', 'homework': '81', 'exam2': '57', 'attendance': '19', 'project2': '42', 'id': '95255664'},
'47718233': {'exam1': '81', 'project1': '55', 'classrecap': '53', 'homework': '46', 'exam2': '46', 'attendance': '4', 'project2': '14', 'id': '47718233'},
'55527760': {'exam1': '34', 'project1': '89', 'classrecap': '39', 'homework': '19', 'exam2': '99', 'attendance': '78', 'pta': 65, 'project2': '99', 'id': '55527760'},
'32548926': {'exam1': '9', 'project1': '7', 'classrecap': '77', 'homework': '98', 'exam2': '1', 'attendance': '43', 'project2': '86', 'id': '32548926'}}
generateReport(d)
with open('gradebook.txt') as f:
x = f.read()
print(x)
你可以,然后有一个版本使用CSV模块,注释掉了
- 我和repl.it没有任何关系,我只是非常喜欢它
- 真的吗?我得到的结果是:
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
将您的代码放入,它会向您显示许多警告中的问题:
您写入文件的唯一位置是第14行和第17行,这两行是学生成绩的循环:
和学生.values()中val的,但您没有在任何地方定义学生
您确实在上一个循环中将其用作循环变量,因此students
具有上一个循环的结束值
要解决这个问题,您需要在成绩册上循环一次,一次得到一个学生,然后对于每个条目,您需要循环学生的数据,以处理文件的行
你的标题“有序”是什么意思?不清楚您是指列还是行,或者两者都应该排序,或者顺序是什么。但是,当您可以使用有序数据结构(元组、列表)时,却使用无序数据结构(字典),这会让您的生活变得更轻松,这让您变得相当愚蠢
(你想要的输出中那些数据中不存在的列呢,比如潜在等级?或者你想要的标题与数据的大小写/间距不同,Python区分大小写,这很烦人?或者csv
模块的存在方式可以为你做很多这方面的工作)
将数据传递到名为成绩册的generateReport
-使用该名称。当你使用d
时,你正在从函数中伸出来,进入父范围,这是一个复杂而棘手的问题,这意味着成绩册
永远不会被使用,这对任何阅读它的人来说都是混乱和无益的
使用有用的名称—student\u id
来迭代学生id,而不是stu
<代码>学生
单数,而不是学生
。任何东西,而不是任何东西
()
关闭outfile以调用close方法def generateReport(gradebook):
outfile=open('gradebook.txt','w')
# all the columns, in the order they need to show up.
columns = ('ID', 'Exam1', 'Exam2', 'Homework', 'Attendance',
'Project1', 'Project2', 'Class Recap',
'Final Grade', 'Potential Grade')
# join the columns into a tab-separated line with a newline, and write it once
header_line = '\t'.join(columns) + '\n'
outfile.write(header_line)
# sort the student IDs into (numeric) order:
sorted_student_ids = sorted(gradebook.keys(), key=int)
# loop over each student ID
for student_id in sorted_student_ids:
# for each student, get their data
student_data = gradebook[student_id]
# build up a line of output for this students, using the columns
student_row = []
for column in columns:
# columns don't match the dictionary keys :|
# remove spaces and make them lowercase, so they do
# also handle where there is no data for a column and get '' instead
# and convert to strings for writing to the file
column = column.replace(' ', '').lower()
value = str(student_data.get(column, ''))
student_row.append(value)
# now we have the data for this student, in the desired column order
student_line = '\t'.join(student_row) + '\n'
outfile.write(student_line)
outfile.close()
d={'37340752': {'exam1': '50', 'project1': '40', 'classrecap': '39', 'homework': '62', 'exam2': '3', 'attendance': '17', 'project2': '86', 'id': '37340752'},
'95255664': {'exam1': '76', 'project1': '60', 'classrecap': '39', 'homework': '81', 'exam2': '57', 'attendance': '19', 'project2': '42', 'id': '95255664'},
'47718233': {'exam1': '81', 'project1': '55', 'classrecap': '53', 'homework': '46', 'exam2': '46', 'attendance': '4', 'project2': '14', 'id': '47718233'},
'55527760': {'exam1': '34', 'project1': '89', 'classrecap': '39', 'homework': '19', 'exam2': '99', 'attendance': '78', 'pta': 65, 'project2': '99', 'id': '55527760'},
'32548926': {'exam1': '9', 'project1': '7', 'classrecap': '77', 'homework': '98', 'exam2': '1', 'attendance': '43', 'project2': '86', 'id': '32548926'}}
generateReport(d)
with open('gradebook.txt') as f:
x = f.read()
print(x)
你可以,然后有一个版本使用CSV模块,注释掉了
- 我和repl.it没有任何关系,我只是非常喜欢它
outfile.close
不会关闭文件。您需要括号:outfile.close()
,或者(更好)将与
块一起使用。您的帖子和代码格式已关闭:请更正以使其可读。请注意,outfile.close
不会关闭文件。您需要括号:outfile.close()
,或者(更好)使用带有块的。这样我可以获得多次[Attention project2 classrecap project1