代码之家  ›  专栏  ›  技术社区  ›  clarkk

Python新增-运算符//[已关闭]

  •  -3
  • clarkk  · 技术社区  · 7 年前

    我找到了一些应该可以工作的代码,但是没有。。我做错了什么/遗漏了什么?

    资料来源: How to find table like structure in image

    def find_table_in_boxes(boxes, cell_threshold=10, min_columns=2):
        for box in boxes:
            (x, y, w, h) = box
            print(box)
            col_key = x // cell_threshold
    
    if __name__ == '__main__':
        text_boxes = [
            {
                'x': 123,
                'y': 512,
                'w': 100,
                'h': 150
            },
            {
                'x': 500,
                'y': 512,
                'w': 100,
                'h': 150
            }
        ]
        cells = find_table_in_boxes(text_boxes)
    

    错误

    # python test.py
    {'x': 123, 'w': 100, 'y': 512, 'h': 150}
    Traceback (most recent call last):
      File "test.py", line 22, in <module>
        cells = find_table_in_boxes(text_boxes)
      File "test.py", line 5, in find_table_in_boxes
        col_key = x // cell_threshold
    TypeError: unsupported operand type(s) for //: 'str' and 'int'
    
    1 回复  |  直到 7 年前
        1
  •  8
  •   kichik    7 年前

    它在告诉你 x 这是一根绳子。它来自 boxes text_boxes . 您不能使用以下方式解压缩字典值:

        (x, y, w, h) = box
    

    x = box["x"]
    y = box["y"]
    w = box["w"]
    h = box["h"]
    

    另一个选择是:

    def _find_table_in_box(cell_threshold, x, y, w, h):
        print(x, y, w, h)
        col_key = x // cell_threshold
    
    
    def find_table_in_boxes(boxes, cell_threshold=10, min_columns=2):
        for box in boxes:
            _find_table_in_box(cell_threshold, **box)