简体   繁体   English

如何在包含字符串的列中查找单元格并在另一行中返回单元格的值?

[英]How to find cell in a column that contains a string and return value of a cell in another of row?

If the cell contains "external" from the C column then copy cell "good" from the D column, into the E column, in the rows where the A column contains 003 .如果单元格包含来自C列的"external" ,则将单元格"good"D列复制到E列中,在A列包含003的行中。

Below are two images (before and after) in excel.以下是excel中的两张图像(之前和之后)。

Before:前:

在此处输入图片说明

After:后:

在此处输入图片说明

I tried to find a correct script but it did not work out.我试图找到一个正确的脚本,但没有成功。 It needs to be changed to "row" and "column" where I put "???"它需要更改为“行”和“列”,我在其中放置“???” :

import openpyxl
from openpyxl import load_workbook
wb_source = openpyxl.load_workbook("path/file.xlsx")
sheet = wb_source['Sheet1']
x=sheet.max_row
y=sheet.max_column

for r in range(1, x+1) :
   for j in range(1, y+1):
       copy(sheet.cell(row= ???, column=???)
         if str(copy.value)=="external":
         sheet.??
         break
wb_source.save("path/file2.xlsx")

How should they be added (row and column)?它们应该如何添加(行和列)?

  1. Read the entire sheet.阅读整张纸。
  2. Create a dictionary for the external products为外部产品创建字典
  3. Write back to Excel.写回 Excel。

Try:尝试:

import openpyxl
wb = openpyxl.load_workbook("file1.xlsx")
ws = wb['Sheet1']

data = list()
for r, row in enumerate(ws.iter_rows()):
    data.append([cell.value for c, cell in enumerate(row)])
mapper = {l[0]: l[-1] for l in data if l[2]=="external"}

for r, row in enumerate(ws.iter_rows()):
    if ws.cell(r+1, 1).value in mapper:
        ws.cell(r+1, 5).value = mapper[ws.cell(r+1, 1).value]
wb.save("file2.xlsx")

暂无
暂无

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

相关问题 Excel:如何在包含字符串的列中查找单元格并在另一列行中返回单元格的值? - Excel: How to find cell in a column that contains a string and return value of a cell in another column of row? 在Pandas数据帧中查找任何单元格值= = x,并返回单元格值,列标题,行和相邻单元格值 - Find any cell values >= x in Pandas dataframe and return cell value, column header, row and neighbouring cell value Pandas:如果同一行上的另一个单元格包含字符串,则将变量设置为单元格中的值 - Pandas: Set variable to value in cell, if another cell on same row contains string 如何检查 dataframe 列是否包含来自另一个 dataframe 列的字符串并返回 python Z3A0524F883225EFFA94 中的相邻单元格 - How do I check if dataframe column contains a string from another dataframe column and return adjacent cell in python pandas? 如何返回某个值的单元格的列和行索引 - How to return column&row index of cell of certain value 如何使用用户输入来选择行列和返回单元格值? - How to use user input to select row column and return cell value? DataFrame:如何根据行的另一个单元格切换单元格值? - DataFrame: How to toggle a cell value base on another cell of the row? 检查csv单元格是否包含字符串并修改Python中的列值 - Check if csv cell contains a string and modify column value in Python 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? 如何迭代每一行(每个单元格)并检查单元格是否包含值或nan,如果nan跳过行 - How to iterate every row(every cell) and check whether the cell contains value or nan, if nan skip row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM