Python / Excel:将给定值与求和值进行比较 [英] Python/Excel: Compare the given value with the summed value

查看:1177
本文介绍了Python / Excel:将给定值与求和值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Excel Image 1



从Excel Image 1中,我将数据拉入列A和列B的python中。目标是对列B的值进行求和,并查看它是否高于或低于给定的总和1500。如果高于或等于1500,则无需做任何事情。但是,如果它较低,则在其旁边调整具有单词change的值,使得计算的和变为1500或更多。在我们的情况下,总和是700,低于1500。

  import xlrd 
excel ='/ Users / Bob / Desktop /'

wb1 = xlrd.open_workbook(excel +'assignment2.xlsx')
sh1 = wb1.sheet_by_index(0)

colA, colB = [],[]
一个范围(3,sh1.nrows):
colA.append(int(sh1.cell(a,0).value))
colB .append(int(sh1.cell(a,1).value))
print(colA)
print(colB)

excel_sum =(sh1.cell_value(2, 1))
print(Given Sum:,excel_sum)

calc_sum = sum(colB)
print(calc_sum)

如果calc_sum> ; = excel_sum:
print(Good)
#else:
#需要调整旁边有change字样的值

当前输出:

  [0,1 ,2,3,4] 
[900,-400,-200,300,100]
给定总和:1500.0
700
pre>

谢谢!

解决方案

你知道delta - excel_sum - calc_总和。所以扫描列C进行更改,并将其添加到列B中。

  else:
3,sh1.nrows):
如果sh1.cell(row,2)=='change':
colB [row] = int(sh1.cell(row,1))+ excel_sum - calc_sum
break

当然,我看到你使用 xlrd 而不是 xlwt ,所以我不知道如何改变价值。你是吗?


Excel Image 1

From "Excel Image 1", I am pulling data into python of Column A and Column B. The goal is to sum the values of Column B, and see whether it is higher or lower than the given sum of 1500. If it is higher or equal to 1500, then there needs to be nothing done. But, if it is lower, then adjust the values that has the word "change" beside them, such that the calculated sum becomes 1500 or more. In our case, the sum is 700, which is lower than 1500.

import xlrd
excel = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(excel + 'assignment2.xlsx')
sh1 = wb1.sheet_by_index(0)

colA,colB = [],[]
for a in range(3,sh1.nrows):
    colA.append(int(sh1.cell(a,0).value))
    colB.append(int(sh1.cell(a,1).value))
print(colA)
print(colB)

excel_sum=(sh1.cell_value(2,1))
print("Given Sum:", excel_sum)

calc_sum = sum(colB)
print(calc_sum)

if calc_sum >= excel_sum:
    print("Good")
#else:
#Need to adjust the values that has the words "change" beside them

Current Output:

[0, 1, 2, 3, 4]
[900, -400, -200, 300, 100]
Given Sum: 1500.0
700

Thanks!

解决方案

You know the delta - excel_sum - calc_sum. So scan column C for a change, and add it to column B.

else:
    for row in range(3, sh1.nrows):
        if sh1.cell(row, 2) == 'change':
            colB[row] = int(sh1.cell(row, 1)) + excel_sum - calc_sum
            break

Of course, I see you using xlrd but not xlwt, so I don't know how you're going to change the value. Do you?

这篇关于Python / Excel:将给定值与求和值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆