用于将HTML表转换为字典列表的Python模块。
table2dicts的Python项目详细描述
表2图片
用于将HTML表转换为字典列表的Python模块。
安装
使用pip:从pypi安装
$ pip install table2dicts
从源安装:
$ python setup.py install
使用量
给它一些带有table
:
>>> table2dicts(''' ... <table> ... <thead> ... <tr><th>a</th><th>b</th><th>c</th></tr> ... </thead> ... <tbody> ... <tr><td>1</td><td>2</td><td>3</td></tr> ... <tr><td>4</td><td>5</td><td>6</td></tr> ... </tbody> ... </table> ... ''') [OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]), OrderedDict([('a', '4'), ('b', '5'), ('c', '6')])]
没有thead
或tbody
,没有问题:
>>> table2dicts(''' ... <table> ... <tr><th>a</th><th>b</th><th>c</th></tr> ... <tr><td>1</td><td>2</td><td>3</td></tr> ... <tr><td>4</td><td>5</td><td>6</td></tr> ... </table> ... ''') [OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]), OrderedDict([('a', '4'), ('b', '5'), ('c', '6')])]
当不存在th
时,第一行td
元素用作头:
>>> table2dicts(''' ... <table> ... <tr><td>a</td><td>b</td><td>c</td></tr> ... <tr><td>1</td><td>2</td><td>3</td></tr> ... <tr><td>4</td><td>5</td><td>6</td></tr> ... </table> ... ''') [OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]), OrderedDict([('a', '4'), ('b', '5'), ('c', '6')])]