Phrogz
9/12/2007 7:00:00 PM
On Sep 12, 12:02 pm, Rupa Ruby <anu.re...@gmail.com> wrote:
> How to read and write a table in ruby
What sort of table? HTML, Database, CSV, Excel?
Assuming you mean HTML, here's a little snippet of script that uses
Hpricot to parse the HTML and convert it to a Ruby Array of Arrays.
html = IO.read( 'file/on/disk.html' )
doc = Hpricot( html )
table = (doc/"table#buglist//tr").map{ |row|
(row/"td[@colspan='']").map{ |cell|
cell.inner_text.strip
}
}.compact
The above finds a table with id="buglist" in the html file, and finds
every cell that does not have a colspan attribute. See Hpricot, XPath,
or other documentation for more information on finding out how to find
the data you want.
To write an HTML table, you can use an existing library, or write out
the text yourself.
More details are needed to help you more.