写一段python代码:从上到下读取excel中B列4行到30行的数据,当出现值为“FAIL”时,输出它对应的A列的值。
时间: 2023-05-27 16:04:43 浏览: 75
import openpyxl
# 打开Excel文件
workbook = openpyxl.load_workbook('example.xlsx')
# 选择要操作的Sheet
sheet = workbook['Sheet1']
# 读取B列4-30行的数据,检查是否存在“FAIL”
for row in sheet.iter_rows(min_row=4, max_row=30, min_col=2, max_col=2):
cell_value = row[0].value
if cell_value == "FAIL":
# 如果出现“FAIL”,输出对应的A列的值
row_num = row[0].row
a_cell = sheet.cell(row=row_num, column=1)
a_value = a_cell.value
print(f"出现了FAIL,对应的A列的值为{a_value}")
相关问题
import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 4] # 仅读取第0列和第4列 chunksize = 100000 # 每次读取100000行数据 for chunk in pd.read_csv(filepath, skiprows=4, encoding="gbk", engine='python', sep=' ', delimiter=None, index_col=False, header=None, skipinitialspace=True, usecols=usecols, chunksize=chunksize): file = np.array(chunk) data = read_message(file) filter_step_size(data) def read_message(file): mask = file[:, 1] == "107" data = file[mask] return data def filter_step_size(data): diff = np.diff(data[:, 0].astype(float)) # 将第0列转换为浮点数类型 mask = np.logical_and(0.090 < diff, diff < 0.110) success_sum = np.count_nonzero(mask) fail_sum = len(mask) - success_sum result = np.column_stack((data[:-1, 0], data[1:, 0], diff)) result = result print("步长通过数: {}".format(success_sum)) print("步长未通过数: {}".format(fail_sum)) print("未通过前一项值:未通过后一项值:差值:") print(result) if name == 'main': read_asc("E:\package\databin 7-12-2023 2-23-05 pm Messages File.asc") 修改这段代码,循环读取xlsx文件中第三列除去第一行的所有数据,将读取的数据依次赋值给read_message方法中的mask,并执行整段代码,每次赋值后打印的结果能明显区分
import numpy as np
import pandas as pd
def read_xlsx(filepath):
df = pd.read_excel(filepath)
data = df.iloc[1:, 2].values
for value in data:
mask = read_message(value)
filter_step_size(mask)
def read_message(value):
mask = value == "107"
return mask
def filter_step_size(mask):
success_sum = np.count_nonzero(mask)
fail_sum = len(mask) - success_sum
result = np.column_stack((mask[:-1], mask[1:]))
result = result.astype(int)
print("步长通过数: {}".format(success_sum))
print("步长未通过数: {}".format(fail_sum))
print("未通过前一项值:未通过后一项值:")
print(result)
if __name__ == '__main__':
read_xlsx("E:\package\databin 7-12-2023 2-23-05 pm Messages File.xlsx")
阅读全文