gpt4 book ai didi

python - 在使用 python 的 excel 中,如何在包含多个单词的单元格中从整列中查找单词?

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:10 26 4
gpt4 key购买 nike

我有一个 Excel 文件,其中 A 列包含关键字列表。

eg. Bob
Dan
Brian

在 B 列中,我有几行,一个单元格中有多个条目:

B1= Bob, Grant, James, Dave
B2= Dean, Dan, Brian

如何将 A 列和 B 单元格中的名称放入 C 列中。即我想要的输出是:

C1= Bob
C2= Dan, Brian

有什么想法吗?我尝试过使用 python,但不知道如何开始。

非常感谢您的帮助

最佳答案

它首先取决于您用于访问 Excel 的 Python 模块。如果您使用的是 Windows,我建议使用 Win32Com,可以在 here. 找到它。该模块使 Python 能够以编程方式访问任何 Microsoft Office 应用程序(包括 Excel),并使用许多与 VBA 中相同的方法。

以下是使用 Win32Com for Excel 时遇到的问题的解决方案。我假设您使用逗号 (',') 作为名称之间的分隔符,并且 A 列和 B 列之间的名称匹配区分大小写(请记住,在 Python 中,“A”不等于“a”)

首先,我们要连接到 Excel 并访问包含您姓名的工作表

#First we need to access the module that lets us connect to Excel
import win32com.client

# Next we want to create a variable that represents Excel
app = win32com.client.Dispatch("Excel.Application")

# Lastly we will assume that the sheet with the names is the active sheet
sheet = app.ActiveSheet

此时,我们有一个名为 sheet 的变量,它代表事件的 Excel 工作表及其名称。您只需单击任何单元格即可激活工作表。现在我们要首先获取 A 列中的所有名称并将其存储到列表中。我们有两种选择:

  1. 我们可以迭代 A 列中的所有单元格,并提取以字符串形式存储的名称,并使用 sheet.Cells(row,col).Value 将其附加到所有名称的列表中。 这对于较小的范围来说效果很好,但如果您处理 500 多行,则可能会很慢。
  2. 我们可以使用sheet.Range("A1","A3").Value 提取A 列中包含名称的整个单元格范围,并将值添加到列表中。如果你有大量的细胞,这会更快。在本示例中,我们将使用 Range。

示例继续:

import win32com.client
app = win32com.client.Dispatch("Excel.Application")
sheet = app.ActiveSheet

# rang is an object representing the cells A1, A2, A3
rang = sheet.Range("A1","A3")

# names is a tuple of length 3 containing tuples of length 2
names = rang.Value

#nameLst is a list of all values in names
nameLst = [name[0] for name in names]

接下来我们要迭代 B 列中的所有名称。为此,我们将使用sheet.Cells.Value 函数来获取 B 列中每个单元格的名称列表。我们还将使用string.split(",") 函数将逗号分隔的名称拆分为名称列表,并使用 string.strip() 删除任何不必要的空格。如果此列表中的任何名称在 nameLst 中,我们就知道我们有一个匹配项,并将其放入 Col C 中。

import win32com.client
app = win32com.client.Dispatch("Excel.Application")
sheet = app.ActiveSheet

rang = sheet.Range("A1","A3")
names = rang.Value
nameLst = [name[0] for name in names]

#Iterate over the rows ic ColB. Remember Excel uses base 1 not 0 for inexing
for rowI in range(1,3):
cellNames = sheet.Cells(rowI,2).Value

#split cellNames by "," and add all of the names to a list.
cellNamesLst = [cellName.strip() for cellName in cellNames.split(",")]

#Now we want a list of all names that are in cellNamesLst and in nameLst
matchLst = [matchName for matchName in cellNamesLst if matchName in nameLst]

#Create a string of all matches to go in Col C
allMatches = ", ".join(matchLst)

#Lastly put all matches in in Col C
sheet.Cells(rowI,3).Value = allMatches

这会将字符串“Bob”放入单元格 C1,将“Dan, Brian”放入单元格 C2。 win32com 的使用非常强大,可用于自动化您在所有 MS Office 应用程序中执行的大部分操作。

这是没有注释的最终代码:

import win32com.client
app = win32com.client.Dispatch("Excel.Application")
sheet = app.ActiveSheet

rang = sheet.Range("A1","A3")
names = rang.Value
nameLst = [name[0] for name in names]

for rowI in range(1,3):
cellNames = sheet.Cells(rowI,2).Value
cellNamesLst = [cellName.strip() for cellName in cellNames.split(",")]
matchLst = [matchName for matchName in cellNamesLst if matchName in nameLst]
allMatches = ", ".join(matchLst)
sheet.Cells(rowI,3).Value = allMatches

希望这有帮助。

关于python - 在使用 python 的 excel 中,如何在包含多个单词的单元格中从整列中查找单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11934108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com