简体   繁体   English

如何使用xlrd从工作表列计算变量?

[英]How to calculate variables from worksheet columns using xlrd?

I am attempting to calculate all variables of a specific value in a given column from an Excel document. 我试图从Excel文档计算给定列中特定值的所有变量。 I want to be able to iterate over the column and calculate the total of each instance... eg how many students received a grade "A". 我希望能够遍历列并计算每个实例的总数...例如,有多少学生获得了成绩“A”。

Here is what I have so far... 这是我到目前为止所拥有的......

test.xls: TEST.XLS:

Name, Class, Grade 姓名,班级,年级

James, Math, A 詹姆斯,数学,A

Judy, Math, A 朱迪,数学,A

Bill, Social Studies, B 比尔,社会研究,B

Denice, History, C 丹尼斯,历史,C

Sarah, History, B 莎拉,历史,B

Here is my python script 这是我的python脚本

import xlrd
from collections import Counter
sh = xlrd.open_workbook('test.xls', on_demand = True).sheet_by_index(0) # Open workbook and sheet

 for rownum in range(sh.nrows):
    grades = str(sh.cell(rownum, 2).value) # Grab all variables in column 2.
    print Counter(grades.split('\n'))  # Count grades

Expected output: 预期产量:

A = 2 A = 2

B = 2 B = 2

C = 1 C = 1

Actual output: 实际产量:

Counter({'Grade': 1}) 柜台({'等级':1})

Counter({'A': 1}) 柜台({'A':1})

Counter({'A': 1}) 柜台({'A':1})

Counter({'B': 1}) 柜台({'B':1})

Counter({'C': 1}) 计数器({'C':1})

Counter({'B': 1}) 柜台({'B':1})

As each grade is showing in a different list I have been unable to merge/concatenate lists to get a total. 由于每个年级显示在不同的列表中,我无法合并/连接列表以获得总计。 Also it is not in the desired output formatting. 它也不是所需的输出格式。

for rownum in range(sh.nrows):
    grades = str(sh.cell(rownum, 2).value) # Grab all variables in column 2.
    print Counter(grades.split('\n'))  # Count grades

You are creating a list in every iteration. 您正在每次迭代中创建一个列表。

You can use list comprehension to a create a single list with all the grades: 您可以使用列表推导来创建包含所有等级的单个列表:

grades = [str(sh.cell(rownum, 2).value) for rownum in range(sh.nrows)]
print Counter(grades)

Or without comprehension : 或者没有理解:

grades = []
for rownum in range(sh.nrows):
    grades.append(str(sh.cell(rownum, 2).value))

print Counter(grades)

You would still need to format the content of Counter(grades) to your liking: 您仍然需要根据自己的喜好格式化Counter(grades)的内容:

res = Counter(grades)
for grade, count in res.iteritems():
    print '{} = {}'.format(grade, count) 

Note that: 注意:

  1. I'm not using split . 我没有使用split

  2. The output won't be in any particular order, and in fact might change between consecutive runs of the script. 输出不会以任何特定顺序排列,实际上可能会在连续运行脚本之间发生变化。

You can start by instantiating a Counter and then add grades to it while you iterate: 您可以首先实例化一个Counter ,然后在迭代时为其添加等级:

grades_counter = Counter()
mysheet = xlrd.open_workbook('grades.xls').sheet_by_index(0)

for i in range(1,mysheet.nrows):
    grades_counter += Counter(str(mysheet.row_values(i)[2]))

print grades_counter
Counter({'A': 2, 'B': 2, 'C': 1})

If you are looking to print the output in a more elegant way, you can do the following: 如果您希望以更优雅的方式打印输出,可以执行以下操作:

for k,v in grades_counter.items():
    print "{} = {}".format(k,v)

You should get: 你应该得到:

A = 2
C = 1
B = 2

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM