简体   繁体   English

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

[英]Retrieving value from Excel table from an intersection of values using Python

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. 我在Excel中有一个表格,我希望能够读取它(我知道如何实现),但还告诉Python如果d,D和B有某些值,它会返回顶部的尺寸值第一行是我程序中的变量。 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): 我的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

If I take an example and I have ad = 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. 如果我举一个例子,我的广告= 2,D = 4(这两个值将始终在同一行中),而B =2。因此,我想将Dimension = 37的值返回给我的程序。 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. 我也有一个问题,我要阅读几个工作表,所以我将此表称为Table1,并且我必须首先通读所有工作表,这些工作表在同一.xls文件中各包含一个表。

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. 您应该可以自己制作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

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. 我没有从字面上回答“ gimme teh codez”的问题。 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. 您必须做一些工作,要么编写一个适合于您用来读取表的工具(可能是xlrd)的“ table_iterator”,要么将其视为伪代码,以作为指导,然后完全重写以适合该表。工具。

"""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?""" “”“最后,我将所有表编译到一个.xls文档中,但使用了多个工作表。是否有一种特殊的方式可以搜索所有工作表,然后应用此代码?”“”

As I said, you should be able to do the loop-over-5-tables stunt yourself. 就像我说的,您应该能够自己制作5表循环特技。 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. 您需要遍历工作表,搜索每个工作表,直到找到匹配的行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM