LoginSignup
0
0

More than 5 years have passed since last update.

pandas で html の table の読み書き

Last updated at Posted at 2018-10-01

html ファイルの作成

html_create.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   html_create.py
#
#                   Oct/01/2018
# ------------------------------------------------------------------
import sys
import pandas as pd
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
df = pd.DataFrame([
        ["t1271","千葉",93416,"2003-11-5"],
        ["t1272","勝浦",47573,"2003-6-14"],
        ["t1273","市原",28451,"2003-9-25"],
        ["t1274","流山",39842,"2003-8-28"],
        ["t1275","八千代",27896,"2003-8-8"],
        ["t1276","我孫子",13175,"2003-1-15"],
        ["t1277","鴨川",85234,"2003-10-21"]])

print(df)
#
path="./cities.html"
#
df.to_html(path,header=None,index=None)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

html ファイルの読み込み

html_read.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   html_read.py
#
#                   Oct/01/2018
# ------------------------------------------------------------------
import sys
import pandas as pd
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_html = "./cities.html"
#
df=pd.read_html(file_html,header=None,encoding='utf8')
print(df[0])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

html ファイル

cities.html
<table border="1" class="dataframe">
  <tbody>
    <tr>
      <td>t1271</td>
      <td>千葉</td>
      <td>93416</td>
      <td>2003-11-5</td>
    </tr>
    <tr>
      <td>t1272</td>
      <td>勝浦</td>
      <td>47573</td>
      <td>2003-6-14</td>
    </tr>
    <tr>
      <td>t1273</td>
      <td>市原</td>
      <td>28451</td>
      <td>2003-9-25</td>
    </tr>
    <tr>
      <td>t1274</td>
      <td>流山</td>
      <td>39842</td>
      <td>2003-8-28</td>
    </tr>
    <tr>
      <td>t1275</td>
      <td>八千代</td>
      <td>27896</td>
      <td>2003-8-8</td>
    </tr>
    <tr>
      <td>t1276</td>
      <td>我孫子</td>
      <td>13175</td>
      <td>2003-1-15</td>
    </tr>
    <tr>
      <td>t1277</td>
      <td>鴨川</td>
      <td>85234</td>
      <td>2003-10-21</td>
    </tr>
  </tbody>
</table>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0