【发布时间】:2016-08-23 04:25:28
【问题描述】:
我做了一个数学测验作为练习。任务 3 的一部分是将包含姓名和分数的 CSV 文件按字母顺序组织。我已经完成了一些代码,但我想知道如何让它像我期望的那样工作。我不太确定我做了什么,所以请你把解释简单一点。
data = input('How would you like the data? a for alphabetically, b for highest score to lowest and c for average score highest to lowest. press q to exit: ')
if 'a':
score_file = open('scorefile.csv')
for x in sorted (score_file, key=str.lower):
print (x)
【问题讨论】:
-
您的代码有效吗?
score_file不是您可以排序的列表,您必须先阅读这些行。 -
@DisplayName 如何更改它以使其更好?
-
也许看看这个:docs.python.org/3/library/csv.html?highlight=csv#csv.reader 一个可能有帮助的 csv 阅读器
-
@jDO 我加了上面运行代码的图片
-
@DisplayName 我添加了上面运行代码的图片
标签: python file sorting python-3.x csv