使用openpyxl读取行值时遇到问题

时间:2016-01-14 16:54:08

标签: python openpyxl

我正在尝试读取两列(A和B)中的行值。

这是我目前的代码:

import openpyxl as op
wb = op.load_workbook ('Canopy\Scripts\De Velliers.xlsx')
ws = wb['Sheet1']; colA = ws.columns[1]; row = ws.row[1]                        
for row in ws.rows:
    for cell in row:
        C_vel = np.array(cell.value) / (A_c * rho)       
wb = op.load_workbook('Canopy\Scripts\De Velliers.xlsx')
ws = wb['Sheet1']; colB = ws.columns[2]; row = ws.row[2]
for row in ws.row:
    for cell in row:
        P_s3 = P_t1 * np.array(cell. value)
        P_t3 = P_s3 + (rho * sp.sqrt(C_vel))/2.
        print (cell. value)
    print "C_vel:",C_vel
    print "P_s3:",P_s3
    print "P_t3:",P_t3
    print"_________" 
wb = op.load_workbook('Canopy\Scripts\De Velliers.xlsx')
ws = wb['Sheet4']
for row in ws.rows:
    for cell in row:
        T_t3 = ((((P_t3 / P_t1)**((k - 1) / k)) - 1) * T_t1)/(cell.value) + T_t1
        T_s3 = T_t3 - (sp.sqrt(C_vel)) / (c_p * 2.)
        rho_ new = P_s3 / (T_s3 * R) 
        print (cell. value)
        print "T_t3:",T_t3
        print "T_s3:", T_s3
        print "rho_new:", rho_new
        print "________"

当我尝试运行它时,我收到以下错误:

     25 wb = op.load_workbook ('Canopy\Scripts\De Velliers.xlsx')
---> 26 ws = wb['Sheet1']; colA = ws.columns[1]; row = ws.row[1]
     27 for row in ws.rows:
     28     for cell in row:
     

属性错误:“工作表”对象没有属性“行”

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

可以通过在几个地方用'rows'替换'row'来避免异常。例如:

...
wb = op.load_workbook('Canopy\Scripts\DeVelliers.xlsx')
ws = wb['Sheet1']
colA = ws.columns[1]
row = ws.rows[1]
for row in ws.rows:
    for cell in row:
        ...