Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: CSV or HTML?

by Tux (Abbot)
on Jan 19, 2012 at 07:21 UTC ( #948706=note: print w/replies, xml ) Need Help??

in reply to CSV or HTML?

When - as suggested - you produce correct (and valid) CSV *), there are utilities available that can convert CSV to XLS or HTML.

e.g. Spreadsheet::Read comes with xlscat that offers the -H option.

$ cat test.csv header,line,1 a,34,12 b,42, c, ,mars $ xlscat -H test.csv >test.html 3 x 4 $

The HTML includes a valid header with CSS, so you can alter the output appearance (with alternating even/odd line coloring)

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " +R/xhtml11/DTD/xhtml11.dtd"> <html xmlns="" xml:lang="en"> <head> <title>test.csv</title> <meta name="Author" content="xlscat 2.1" /> <style type="text/css"><!-- body, h2, td, th { font-family: "Nimbus Sans L", "DejaVu Sans", Helvetica, Arial, sans; } table { border-spacing: 2px; border-collapse: collapse; } td, th { vertical-align: top; padding: 4px; } table > tbody > tr > th, table > tr > th { background: #e0e0e0; } table > tbody > tr > td:not([class]), table > tr > td:not([class]) { background: #f0f0f0; } .odd { background: #e0e0e0; } --></style> </head>



*) Producing valid CSV with Text::CSV_XS or Text::CSV is easy, use print as demonstrated in this example.

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: CSV or HTML?
by tobyink (Abbot) on Jan 19, 2012 at 10:47 UTC

    Shame it wraps the CSS in that cargo cult <!-- --> nonsense. This was kinda useful for a few browsers in the mid-1990s, but is actively harmful today.

    In XML DTDs (including the DTD for XHTML 1.1), there is no mechanism to specify that the content model for an element is CDATA. Thus the <!-- --> acts as a genuine XML comment, and causes the style sheet contained within it to be commented out and totally ignored.

    If a browser is in HTML mode (i.e. you serve the page using Content-Type: text/html) you won't notice this, but as soon as you switch to Content-Type: application/xhtml+xml all your styles will disappear.


    <html xmlns=""> <head> <title>CSS demo</title> <style type="text/css"> p { color: green } </style> <style type="text/css"><!-- p.mine { color: red } --></style> </head> <body> <p class="mine">This should be green.</p> </body> </html>

    The above, viewed in a standards compliant browser, in XHTML mode, will show a green paragraph. The CSS which sets it to red is commented out and ignored.

      ...and the name of "a standards compliant browser" (to which to your refer) is?

      I ask because I don't really believe there is such a thing.

      Tangent: Using the phrase "standards compliant" (yeah, I'm guilty too) tends to obscure the issue -- IMO -- which arises from the fact that "standards compliant" is not the same as "implements all of whichever relevant standard one might select."

      There is, for example, a "standard" (and right now I've forgotten whether its CSS or html4.1) providing a way to align a mixed length set of decimal numbers*1 in a column of <td>s in a <table> (it applies to other things too, but that's easy to grasp.

      That would be a very handy standard to follow... except that no browser (of which I'm aware) actually implements it. Yes, there are workarounds, but as is often the case, those workarounds are often a PITA.

      *1 Example of the mixed length set of:


      One common workaround, aligning the column rightward, doesn't work there, but some means of aligning the decimal points sure would make it easier to read.

        ...and the name of "a standards compliant browser" (to which to your refer) is?

        I ask because I don't really believe there is such a thing.

        There is only one reference browser: amaya. But you'd be a serious masochist if you actually use it :).

        I'm curious what that mixed alignment setting would be: I cannot find it in my CSS3 reference.

        Enjoy, Have FUN! H.Merijn

        ...and the name of "a standards compliant browser" (to which to your refer) is?

        For the purpose of my example, any version of Opera, Mozilla or Safari released in the last decade should do, as should IE9.

        Sure there are plenty of holes in standards compliance in browsers, but this particular standard (parsing XHTML) is pretty reliably implemented.

        I believe that something along these lines would have worked at one time...

        <style> .mlx-test-align tr td { text-align: "."; } </style> <table class="mlx-test-align"> <tr><td>1.01234</td></tr> <tr><td>101.234</td></tr> <tr><td>101.00</td></tr> <tr><td>10123.4</td></tr> </table>
        ... but according to the blooberry CSS reference (IME, a fairly reliable summary of the various aspects of the CSS and HTML specs),
        The [string] value type has been removed in CSS2.1.
        I was not able to produce the correct alignment using text-align, but that may speak more of my capabilities than the capabilities of my browsers.

        Rather a shame, as aligning to a character has some useful applications.

        Update: It appears that another blooberry page has some possibly updated information (testing below...)

        Example from
        Vegetable Cost per kilo
        Lettuce $1
        Silver carrots $10.50
        Golden turnips $100.30

        I am not able to reproduce their results. My guess is that I didn't set the document type correctly.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948706]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-05-22 22:04 GMT
Find Nodes?
    Voting Booth?