Pandas read_html 无法正确读取文本

我有以下文字:


text = """<table class="table table-striped">\n                            <thead>\n                                <tr>\n                                    <th data-field="placement">Placement</th>\n                                    <th data-field="production">Production</th>\n                                    <th data-field="application">Eng.Vol.</th>\n                                    <th data-field="body">Body No</th>\n                                    <th data-field="eng">Eng No</th>\n                                    <th data-field="eng">Notes</th>\n                                </tr>\n                                                                <tr>\n                                    <td data-field="placement">Front Stabilizer</td>\n                                    <td data-field="production">Oct 16~</td>\n                                                                        <td data-field="application">1.5 L</td>\n                                    <td data-field="body">HRW18</td>\n                                    <td data-field="eng">L15BY</td>\n                                    <td data-field="note" class="">\n                                        Pos:Left/Right </td>\n                                </tr>\n                                                                <tr>\n                                    <td data-field="placement">Front Stabilizer</td>\n                                    <td data-field="production">Oct 16~</td>\n                

此 HTML 文本使用表格标签正确关闭,并且具有所有必需的标签。pandas 仍然没有作为表格来读取。


慕容708150
浏览 43回答 1
1回答

杨__羊羊

你的桌子被包裹在里面<thead></thead>。熊猫将所有内容解释为列是可以理解的。咱们试试吧:tmp=pd.read_html(text)[0]pd.DataFrame(tmp.columns.to_frame().values)输出:&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4--&nbsp; ----------&nbsp; ----------------&nbsp; ----------------&nbsp; ----------------&nbsp; ----------------&nbsp;0&nbsp; Placement&nbsp; &nbsp;Front Stabilizer&nbsp; Front Stabilizer&nbsp; Front Stabilizer&nbsp; Front Stabilizer&nbsp;1&nbsp; Production&nbsp; Oct 16~&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Oct 16~&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Oct 16~&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Oct 16~&nbsp;2&nbsp; Eng.Vol.&nbsp; &nbsp; 1.5 L&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.5 L&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.5 L&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.0 L&nbsp;3&nbsp; Body No&nbsp; &nbsp; &nbsp;HRW18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HRW18 LHD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HRW28&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HRW38 RHD&nbsp;4&nbsp; Eng No&nbsp; &nbsp; &nbsp; L15BY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L15BY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L15BY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R20A9&nbsp;5&nbsp; Notes&nbsp; &nbsp; &nbsp; &nbsp;Pos:Left/Right&nbsp; &nbsp; Pos:Left/Right&nbsp; &nbsp; Pos:Left/Right&nbsp; &nbsp; Pos:Left/Right
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python