python 读取不同的列,判断然后将结果写入excel

# -*- coding:utf-8 -*-

#将excel中数据转换成列表,然后对比,然后对比结果放到excel中
import xlrd
from xlutils import copy
excel_path = 'D:/abnormal.xls' # 文件路径
rbook = xlrd.open_workbook(excel_path)
w_sheet = rbook.sheet_by_name(u'sheet1')
rowNum = w_sheet.nrows

cols_data=w_sheet.col(1, start_rowx=1, end_rowx=None) #第2列所有数据
list_110 = []
for a_row in cols_data:
# 遍历每一行的单元格
list_110.append(a_row.value)
print('xxxxx:',list_110)


cols_data_new=w_sheet.col(2, start_rowx=1, end_rowx=None) #第3列所有数据
list_110_new = []
for a_row in cols_data_new:
# 遍历每一行的单元格
list_110_new.append(a_row.value)
print('xxxxx:',list_110_new)


wbook = copy.copy(rbook)    #在已经存在的excel中写入数据
w_sheet1 = wbook.get_sheet(0)
w_sheet1.write(0, 3, '结果')
i=0
for i in range(rowNum):
if (list_110[i]!='')&(list_110_new[i]!=''):
if float(list_110[i])>=float(list_110_new[i]):
w_sheet1.write(i+1, 3, 'Pass')
else:
w_sheet1.write(i+1, 3, 'Fail')
else:
w_sheet1.write(i+1, 3, 'N/A')

wbook.save(excel_path)

posted @ 2021-11-08 20:11  小橙子2018  阅读(242)  评论(0编辑  收藏  举报