3
$\begingroup$

Consider importing the following table from an HTML string:

ImportString["<table>
   <tr>
     <th>Firstname</th>
     <th>Lastname</th> 
     <th>Age</th>
   </tr>
   <tr>
     <td>Jill</td>
     <td>Smith</td> 
     <td>50</td>
   </tr>
 </table>", {"HTML", "Data"}]

{{"Firstname", "Lastname", "Age"}, {"Jill", "Smith", 50}}

above, all fields are imported and we end up with a proper 2x3 table.

However, in a case where one of the entries is empty, we instead get:

ImportString["<table>
   <tr>
     <th>Firstname</th>
     <th>Lastname</th> 
     <th>Age</th>
   </tr>
   <tr>
     <td> </td>
     <td>Smith</td> 
     <td>50</td>
   </tr>
 </table>", {"HTML", "Data"}]

{{"Firstname", "Lastname", "Age"}, {"Smith", 50}}

Here the "Firstname" field was left empty in the table, and so mathematica did not register that field. The resulting list now has 3 entries in the first dimension and only 2 entries in the second, so that the whole table structure is broken. I would prefer the output to instead read e.g.

{{"Firstname", "Lastname", "Age"}, {"","Smith", 50}}

so that table dimensions are preserved.

Is there a way to import table data from HTML string without skipping empty fields?

$\endgroup$

1 Answer 1

5
$\begingroup$

HTML >> Import Elements

enter image description here

So you can use "FullData" in place of "Data":

ImportString["<table>
    <tr>
      <th>Firstname</th>
      <th>Lastname</th> 
      <th>Age</th>
    </tr>
    <tr>
      <td> </td>
      <td>Smith</td> 
      <td>50</td>
    </tr>
  </table>", {"HTML", "FullData"}]

{{{{"Firstname", "Lastname", "Age"}, {"", "Smith", 50}}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.