How to parse table with rowspan and colspan

后端 未结 3 1902
慢半拍i
慢半拍i 2020-12-15 08:51

First, I have read Parsing a table with rowspan and colspan. I even answered the question. Please read before you mark this as duplicate.



        
3条回答
  •  伪装坚强ぢ
    2020-12-15 08:54

    Important to note, that Martijn Pieters solution does not account for the case of cells having rowspan and colspan attribute simultaneosly. E.g.

    A B C D
    E
    E C C
    E C C C C C

    This table renders to

    +-----------+---+---+---+
    | A         | B | C | D |
    |           +---+---+---+
    |           | E         |
    |           +---+---+---+
    |           | E | C | C |
    +---+---+---+---+---+---+
    | E | C | C | C | C | C |
    +---+---+---+---+---+---+
    

    but if we apply the function we get

    [['A', 'A', 'A', 'B', 'C', 'D'],
     ['A', 'E', 'E', 'E', None, None],
     ['A', 'E', 'C', 'C', None, None],
     ['E', 'C', 'C', 'C', 'C', 'C']]
    

    There may be some edge cases, but extending rowspan bookkeeping to cells in the product of rowspan and colspan, i.e.

       for drow, dcol in product(range(rowspan), range(colspan)):
                try:
                    table[row + drow][col + dcol] = value
                    rowspans[col + dcol] = rowspan
                except IndexError:
                    # rowspan or colspan outside the confines of the table
                    pass
    

    seems to work on examples in this thread, and for the table above it will output

    [['A', 'A', 'A', 'B', 'C', 'D'],
     ['A', 'A', 'A', 'E', 'E', 'E'],
     ['A', 'A', 'A', 'E', 'C', 'C'],
     ['E', 'C', 'C', 'C', 'C', 'C']]
    

提交回复
热议问题