How to generate CSV from HTML with Sledge.

1,900 views

Published on

turbogears#1.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,900
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

How to generate CSV from HTML with Sledge.

  1. 1. Generate CSV with Sledge. Tokuhiro Matsuno Mobile Factory, Inc. 2006-01-15(Sun) Turbogears#1
  2. 2. Generate HTML <ul><li><table id=&quot;downloadable&quot;> </li></ul><ul><li><tr><th>date</th> </li></ul><ul><li><th>PV</th> </li></ul><ul><li><th>UU</th></tr> </li></ul><ul><li><tr> </li></ul><ul><li><td>2007-01-01</td> </li></ul><ul><li><td>77</td> </li></ul><ul><li><td>416</td></tr> </li></ul><ul><li> <!-- snip --> </li></ul><ul><li></table> </li></ul>
  3. 3. We want CSV!! <ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul><ul><li>Excel!!! </li></ul>
  4. 4. How to generate a CSV from HTML <ul><li>ElementTree </li></ul>
  5. 5. What’s ElementTree <ul><li>The Element type is a simple but flexible container object, designed to store hierarchical data structures, such as simplified XML infosets, in memory. The element type can be described as a cross between a Python list and a Python dictionary. </li></ul>
  6. 6. ElementTree <ul><li>Python 2.5 </li></ul><ul><li>Xpath… Orz </li></ul>
  7. 7. How to use the ElementTree <ul><li>from elementtree import ElementTree </li></ul><ul><li>import csv, sys </li></ul><ul><li>tree = ElementTree.parse('report.xhtml') </li></ul><ul><li>writer = csv.writer(sys.stdout) </li></ul>
  8. 8. How to use the ElementTree(2) <ul><li>for table in tree.getiterator(): </li></ul><ul><li>if (table.tag.endswith('table') and table.get('id') == 'downloadable‘): </li></ul><ul><li>for tr in table.getchildren(): </li></ul><ul><li>writer.writerow( </li></ul><ul><li>[td.text for td in tr.getchildren()] </li></ul><ul><li> ) </li></ul>
  9. 9. peroration <ul><li>Generate CSV from HTML. </li></ul><ul><li>Cost down.Cost down.Cost down. </li></ul>

×