使用 Python 从值的交集中检索 Excel 表中的值

发布于 2024-12-17 11:58:26 字数 695 浏览 2 评论 0原文

我在 Excel 中有一个表格,我希望能够读取它(我知道如何实现这一点),但也告诉 Python,如果 d、D 和 B 存在某些值,则它返回顶部的维度值第一行作为我的程序中的变量。我读过有关交叉路口的各种帖子,我不确定它们是否是我正在寻找的内容,因此我决定问自己的问题。

我的Excel表格的格式如下(只是一个小例子,因为我无法发布图像):

                      Dimensions
d      D  |  17      27    37     47       17-47
          |                B           |   rsmin
0.6    2     0.8     -     -       -       0.05
1    2.5     1       -     -       -       0.05
1.5    3     1       -     1.8     -       0.05
2      4     1.2     -     2       -       0.05

如果我举一个例子,我有ad = 2,D = 4(这两个值将始终位于同一行) B = 2。因此,我想将值 Dimension = 37 返回到我的程序中。我还遇到一个问题,即我有多个工作表需要阅读,因此我将此表称为 Table1,并且我必须首先通读所有工作表,其中每个工作表都包含在同一 .xls 文件中。

I have a table in Excel and I want to be able to read it (I know how to achieve this) but also tell Python that if there are certain values of d, D and B, for it to return the dimension value in the top first row as a variable in my programme. I have read various posts concerning intersections and I am not sure whether they were what I was looking for therefore I have decided to ask my own question.

My Excel table is in the format as follows (just a small example since I can't post images):

                      Dimensions
d      D  |  17      27    37     47       17-47
          |                B           |   rsmin
0.6    2     0.8     -     -       -       0.05
1    2.5     1       -     -       -       0.05
1.5    3     1       -     1.8     -       0.05
2      4     1.2     -     2       -       0.05

If I take an example and I have a d = 2, D = 4 (these two values will always be in the same row) and B = 2. I therefore would like to return the value Dimension = 37 to my programme. I also have the problem that I have several worksheets to read so I will refer to this table as Table1 and I must initially read through all worksheets which include one table each in the same .xls file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-12-24 11:58:26

以下是如何根据对您想要的内容的猜测来搜索您的表格。您应该能够自己完成“循环 5 个表”特技。

def search(table_iterator, d, D, B):
    headings = next(table_iterator)
    junk = next(table_iterator)
    key = [d, D]
    for row in table_iterator:
        if row[0:2] != key: continue
        for index, value in enumerate(row[2:-1]):
            if value == B: 
                return headings(2 + index)
    return None

在评论中提出问题后更新:

“”“这应该返回我想要的维度值?”“”

是的,它应该。但这是一种通用方法。我不会从字面上回答“给我代码”的问题。您必须做一些工作,要么编写一个适合您用来读取表的工具(大概是 xlrd)的“table_iterator”,要么将其视为伪代码,您将其作为指南并完全重写以适应工具。

“”“最后,我将所有表格编译到一个 .xls 文档中,但使用了多个工作表。是否有一种特定的方法可以搜索所有工作表,然后应用此代码?”“”

正如我所说,您应该能够自己做循环 5 个表的特技。搜索所有工作表,然后应用此代码似乎是一种奇怪的方法。您需要迭代工作表,搜索每个工作表,直到找到匹配的行。

Here's how to search your table, based on guesses as to what you want. You should be able to do the loop-over-5-tables stunt yourself.

def search(table_iterator, d, D, B):
    headings = next(table_iterator)
    junk = next(table_iterator)
    key = [d, D]
    for row in table_iterator:
        if row[0:2] != key: continue
        for index, value in enumerate(row[2:-1]):
            if value == B: 
                return headings(2 + index)
    return None

Update after questions asked in comment:

"""This should return the dimension value I want?"""

Yes, it should. But it's a generalised approach. I don't answer "gimme teh codez" questions literally. You have to do some work, either to write a "table_iterator" suitable to the tool (presumably xlrd) with which you are reading the table(s), or treat it as pseudocode which you take as a guide and completely rewrite to suit the tool.

"""In the end I have compiled all my tables into one .xls document but using several worksheets. Is there a particular way I could go about searching all worksheets and then applying this code?"""

As I said, you should be able to do the loop-over-5-tables stunt yourself. Searching all worksheets and then applying this code seems a strange approach. You need to iterate over the worksheets, searching each one, until you find a matching row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文