Re: TSV to HTML



> I was wondering if anyone here on the group could point me
> in a direction that would expllaing how to use python to
> convert a tsv file to html. I have been searching for a
> resource but have only seen information on dealing with
> converting csv to tsv. Specifically I want to take the
> values and insert them into an html table.
>
> I have been trying to figure it out myself, and in
> essence, this is what I have come up with. Am I on the
> right track? I really have the feeling that I am
> re-inventing the wheel here.
>
> 1) in the code define a css
> 2) use a regex to extract the info between tabs
> 3) wrap the values in the appropriate tags and insert into
> table.
> 4) write the .html file

Sounds like you just want to do something like

print "<table>"
for line in file("in.tsv"):
print "<tr>"
items = line.split("\t")
for item in items:
print "<td>%s</td>" % item
print "</tr>"
print "</table>"

It gets a little more complex if you need to clean each item
for HTML entities/scripts/etc...but that's usually just a
function that you'd wrap around the item:

print "<td>%s</td>" % escapeEntity(item)

using whatever "escapeEntity" function you have on hand.
E.g.

from xml.sax.saxutils import escape
:
:
print "<td>%s</td>" % escape(item)

It doesn't gracefully attempt to define headers using
<thead>, <tbody>, and <th> sorts of rows, but a little
toying should solve that.

-tim





.



Relevant Pages

  • Re: HTML Table Question
    ... that would tell the object not to wrap to the next line. ... Still have no idea what HTML this "asp:Menu" represents, if you are *not* going to supply a URL then at least insert what the ASP output would be... ... different font size then the text will not fit within your containers and the design will break. ... Now <=IE6 will not support "min-width" and there are some workarounds that you can Google for, ...
    (alt.html)
  • Re: Showing all spaces without using the &nbsp;
    ... Some lines in code (such as HTML ... tags with a lot of attributes) will be too long to fit on one line, ... PRE tag prevents text wrap. ... >>used the Server.HTMLEncodemethod to display it as it is in the file. ...
    (microsoft.public.inetserver.asp.general)
  • Re: Control Z-Order of text and images in importing HTML documents to Word
    ... CSS and set behind the text using Z-Index. ... the graphic "wrap" setting is by defult changed ... The letterhead has to be used graphically because of its unique shape, ... an HTML editor; the only HTML it opens "predictably" is its own ...
    (microsoft.public.word.pagelayout)
  • Re: 10.4 Mail: Text Not Wrapping
    ... paragraphs no longer wrapped to fit the ... because the lines would in fact wrap within a reply window even ... > It doesn't appear to be an HTML vs. plain-text thing, ...
    (comp.sys.mac.apps)
  • Re: Can you turn off automatic text wrapping?
    ... > Any fix for line wraps using HTML format? ... > I need to wrap manually, which is a pain. ... Entourage FAQ Page: ...
    (microsoft.public.mac.office.entourage)