繁体   English   中英

从Excel阅读专栏并做特定的数学运算

[英]reading column from excel and doing specific math

我有一个具有27列的Excel文件。 我想编写一个python代码,逐列读取并将最终的5个值存储在coloum中,这将对它们完成数学方程式处理。

到目前为止,我有:

from math import tan

#Write Header
#outFile.write('Test Name,X+ avg,X+ std,X+ count,X- avg,X- std,X- count,X angle,Y+ avg,Y+ std,Y+ count,Y- avg,Y- std,Y- count,Y angle\n')

#for line in inFile:


if 1==1:

    line = [1,2,38.702,37.867,35.821, 44, 49,55,65,20,25,28,89.]

    line0= len(line)
    print "the list size"
    print line0

    line1 = len(line) -5  #takes the overall line and subtracts 5. 
    print "the is the start of the 5 #'s we need"
    print line1 #prints the line

    d= line[line1:line0]       #pops all the values from in the line (..)
    print "these are the 5 #'s we need"
    print d

    lo=min(d)
    hi=max(d)


    print "the lowest value is"
    print lo


    print "the highest value is"
    print hi

    average1 = float (sum(d))/ len(d)   #sum
    print "the average of the values is"
    print average1

我想让python自动在列中读取“行= [1,2,38.702,37.867,35.821,44,49,55,65,20,25,28,89。]”部分,存储5个最后一个值和做以上数学分析。

您可能想要使用像openpyxl这样的库,这是docs中有关如何访问某些单元格的示例。

columnOffset = 2 # column offset is (the letter as a number - 1), a = (1-1), b = (2-1)
rowOffset = 35 # row offset is row number - 1

wb = load_workbook('test.xlsx')
ws = wb.get_active_sheet()
for column in ws.columns[columnOffset:27]: #because you have 27 columns to parse through
    for rowIndex, cell in enumerate(column[-5:]):
        if rowIndex >= rowOffset:
            print cell.value

不幸的是,openpyxl对列没有很好的支持,因此您必须使用索引来获取列。

基于xlrd

import xlrd

wb = xlrd.open_workbook("C:\Users\Asus\Desktop\herp.xls")
sh = wb.sheet_by_name(u'A Snazzy Title')

for index in xrange(2, 4):
    lastFive = sh.col_values(index, start_rowx=59, end_rowx=64)
    print lastFive

看起来xlrd将整个电子表格视为2D列表,因此请记住将索引从0开始。是的xlrd完全基于python。

感谢Alexis指出此模块。

仅供参考,还有python-excel项目( http://www.python-excel.org/ )中的xlrd / xlwt 不过,您还需要下载它。 我从未使用过openpyxl,所以我不能说哪个更好。

暂无
暂无

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

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