python 处理table

# -*- coding: utf-8 -*-
from collections import defaultdict


def table_to_list(table):
    dct = table_to_2d_dict(table)
    return list(iter_2d_dict(dct))


def table_to_2d_dict(table):
    result = defaultdict(lambda : defaultdict(str))
    for row_i, row in enumerate(table.xpath('./tr')):
        for col_i, col in enumerate(row.xpath('./td|./th')):
            colspan = int(col.get('colspan', 1))
            rowspan = int(col.get('rowspan', 1))
            col_data = "".join(col.xpath('.//text()')).replace(" ", "").replace("\n", "")
            while row_i in result and col_i in result[row_i]:
                col_i += 1
            for i in range(row_i, row_i + rowspan):
                for j in range(col_i, col_i + colspan):
                    result[i][j] = col_data
    return result


def iter_2d_dict(dct):
    for i, row in sorted(dct.items()):
        cols = []
        for j, col in sorted(row.items()):
            cols.append(col)
        print(len(cols), cols)
        yield cols


if __name__ == '__main__':

    test_html = """
    <div id="head"></div><!-- head finish --><!-- 中间部分 -->
    """
    from lxml import etree
    import pandas as pd

    doc = etree.HTML(test_html.replace("<tbody>", '').replace("</tbody>", '').replace("<thead>", '').replace("</thead>", '').replace("\xa0", ''))
    for table_el in doc.xpath('//table'):
        table = table_to_list(table_el)
        # 表格合并后,会有重复行,可对重复的行,进行去重
        new_table = [""]
        for _list in table:
            if _list != new_table[-1]:
                new_table.append(_list)
        new_table = new_table[1:]
        print(table)
        df = pd.DataFrame(new_table)
        for i in range(df.shape[1]):
            values = [_[0] for _ in df.iloc[:, i:i+1].values.tolist() if _ != ['']]
            if len(values) > 1 and (values[0] == '业主单位' or values[0] == '建设单位'):
                print("miss  list ", values[1:])

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值