TSV to HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    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

    Thanks again for your patience,
    Brian

  • Tim Chase

    #2
    Re: TSV to HTML

    > I was wondering if anyone here on the group could point me[color=blue]
    > 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[/color]

    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(it em)

    using whatever "escapeEnti ty" function you have on hand.
    E.g.

    from xml.sax.saxutil s 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





    Comment

    • Dan M

      #3
      Re: TSV to HTML

      > 1) in the code define a css[color=blue]
      > 2) use a regex to extract the info between tabs[/color]

      In place of this, you might want to look at

      Around the middle of that page you'll see how to use a delimiter other
      than a comma
      [color=blue]
      > 3) wrap the values in the appropriate tags and insert into table. 4)
      > write the .html file
      >
      > Thanks again for your patience,
      > Brian[/color]

      Comment

      • Leif K-Brooks

        #4
        Re: TSV to HTML

        Brian wrote:[color=blue]
        > 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.[/color]

        import csv
        from xml.sax.saxutil s import escape

        def tsv_to_html(inp ut_file, output_file):
        output_file.wri te('<table><tbo dy>\n')
        for row in csv.reader(inpu t_file, 'excel-tab'):
        output_file.wri te('<tr>')
        for col in row:
        output_file.wri te('<td>%s</td>' % escape(col))
        output_file.wri te('</tr>\n')
        output_file.wri te('</tbody></table>')

        Usage example:
        [color=blue][color=green][color=darkred]
        >>> from cStringIO import StringIO
        >>> input_file = StringIO('"foo" \t"bar"\t"baz"\ n'[/color][/color][/color]
        .... '"qux"\t"quux"\ t"quux"\n')[color=blue][color=green][color=darkred]
        >>> output_file = StringIO()
        >>> tsv_to_html(inp ut_file, output_file)
        >>> print output_file.get value()[/color][/color][/color]
        <table><tbody >
        <tr><td>foo</td><td>bar</td><td>baz</td></tr>
        <tr><td>qux</td><td>quux</td><td>quux</td></tr>
        </tbody></table>

        Comment

        • Brian

          #5
          Re: TSV to HTML


          First let me say that I appreciate the responses that everyone has
          given.

          A friend of mine is a ruby programmer but knows nothing about python.
          He gave me the script below and it does exactly what I want, only it is
          in Ruby. Not knowing ruby this is greek to me, and I would like to
          re-write it in python.

          I ask then, is this essentially what others here have shown me to do,
          or is it in a different vein all together?

          Code:

          class TsvToHTML
          @@styleBlock = <<-ENDMARK
          <style type='text/css'>
          td {
          border-left:1px solid #000000;
          padding-right:4px;
          padding-left:4px;
          white-space: nowrap;
          }
          .cellTitle {
          border-bottom:1px solid #000000;
          background:#fff fe0;
          font-weight: bold;
          text-align: center;
          }
          .cell0 { background:#eff 1f1; }
          .cell1 { background:#f8f 8f8; }
          </style>
          ENDMARK

          def TsvToHTML::wrap Tag(data,tag,mo difier = "")
          return "<#{tag} #{modifier}>" + data + "</#{tag}>\n"
          end # wrapTag

          def TsvToHTML::make Page(source)
          page = ""
          rowNum = 0
          source.readline s.each { |record|
          row = ""
          record.chomp.sp lit("\t").each { |field|
          # replace blank fields with &nbsp;
          field.sub!(/^$/,"&nbsp;")
          # wrap in TD tag, specify style
          row += wrapTag(field," td","class=\" " +
          ((rowNum == 0)?"cellTitle": "cell#{rowN um % 2}") +
          "\"")
          }
          rowNum += 1
          # wrap in TR tag, add row to page
          page += wrapTag(row,"tr ") + "\n"
          }
          # finish page formatting
          [ [ "table","cellpa dding=0 cellspacing=0 border=0" ], "body","htm l"
          ].each { |tag|
          page = wrapTag(@@style Block,"head") + page if tag == "html"
          page = wrapTag(page,*t ag)
          }
          return page
          end # makePage
          end # class

          # stdin -> convert -> stdout
          print TsvToHTML.makeP age(STDIN)

          Comment

          • Paddy

            #6
            Re: TSV to HTML

            Brian wrote:[color=blue]
            > First let me say that I appreciate the responses that everyone has
            > given.
            >
            > A friend of mine is a ruby programmer but knows nothing about python.
            > He gave me the script below and it does exactly what I want, only it is
            > in Ruby. Not knowing ruby this is greek to me, and I would like to
            > re-write it in python.
            >
            > I ask then, is this essentially what others here have shown me to do,
            > or is it in a different vein all together?
            >[/color]
            Leif's Python example uses the csv module which understands a lot more
            about the peculiarities of the CSV/TSV formats.
            The Ruby example prepends a <style>...</style> block.

            The Ruby example splits each line to form a table row and each row on
            tabs, to form the cells.

            The thing about TSV/CSV formats is that their is no one format. you
            need to check how your TSV creator generates the TSV file:
            Does it put quotes around text fields?
            What kind of quotes?
            How does it represent null fields?
            Might you get fields that include newlines?

            - P.S. I'm not a Ruby programmer, just read the source ;-)

            Comment

            • Brian

              #7
              Re: TSV to HTML


              Dennis,

              Thank you for that response. Your code was very helpful to me. I
              think that actually seeing how it should be done in Python was a lot
              more educational than spending hours with trial and error.

              One question (and this is a topic that I still have trouble getting my
              arms around). Why is the text in STYLEBLOCK tripple quoted?

              Thanks again,
              Brian

              Comment

              • Scott David Daniels

                #8
                Re: TSV to HTML

                Brian wrote:[color=blue]
                > One question (and this is a topic that I still have trouble getting my
                > arms around). Why is the text in STYLEBLOCK tripple quoted?[/color]

                Because triple-quoted strings can span lines and include single quotes
                and double quotes.

                --
                --Scott David Daniels
                scott.daniels@a cm.org

                Comment

                • Brian

                  #9
                  Re: TSV to HTML


                  Dennis Lee Bieber wrote:[color=blue]
                  > On 1 Jun 2006 03:29:35 -0700, "Brian" <bnblazer@gmail .com> declaimed the
                  > following in comp.lang.pytho n:
                  >[color=green]
                  > > Thank you for that response. Your code was very helpful to me. I
                  > > think that actually seeing how it should be done in Python was a lot
                  > > more educational than spending hours with trial and error.
                  > >[/color]
                  > It's not the best code around -- I hacked it together pretty much
                  > line-for-line from an assumption of what the Ruby was doing (I don't do
                  > Ruby -- too much PERL idiom in it)
                  >[color=green]
                  > > One question (and this is a topic that I still have trouble getting my
                  > > arms around). Why is the text in STYLEBLOCK tripple quoted?
                  > >[/color]
                  > Triple quotes allow: 1) use of single quotes within the block
                  > without needing to escape them; 2) allows the string to span multiple
                  > lines. Plain string quoting must be one logical line to the parser.
                  >
                  > I've practically never seen anyone use a line continuation character
                  > in Python. And triple quoting looks cleaner than parser concatenation.
                  >
                  > The alternatives would have been:
                  >
                  > Line Continuation:
                  > STYLEBLOCK = '\n\
                  > <style type="text/css">\n\
                  > td {\n\
                  > border-left:1px solid #000000;\n\
                  > padding-right:4px;\n\
                  > padding-left:4px;\n\
                  > white-space: nowrap; }\n\
                  > .cellTitle {\n\
                  > border-bottom:1px solid #000000;\n\
                  > background:#fff fe0;\n\
                  > font-weight: bold;\n\
                  > text-align: center; }\n\
                  > .cell0 { background:#3ff 1f1; }\n\
                  > .cell1 { background:#f8f 8f8; }\n\
                  > </style>\n\
                  > '
                  > Note the \n\ as the end of each line; the \n is to keep the
                  > formatting on the generated HTML (otherwise everything would be one long
                  > line) and the final \ (which must be the physical end of line)
                  > signifying "this line is continued". Also note that I used ' rather than
                  > " to avoid escaping the " on text/css.
                  >
                  > Parser Concatenation:
                  > STYLEBLOCK = (
                  > '<style type="text/css">\n'
                  > "td {\n"
                  > " border-left:1px solid #000000;\n"
                  > " padding-right:4px;\n"
                  > " padding-left:4px;\n"
                  > " white-space: nowrap; }\n"
                  > ".cellTitle {\n"
                  > " border-bottom:1px solid #000000;\n"
                  > " background:#fff fe0;\n"
                  > " font-weight: bold;\n"
                  > " text-align: center; }\n"
                  > ".cell0 { background:#3ff 1f1; }\n"
                  > ".cell1 { background:#f8f 8f8; }\n"
                  > "</style>\n"
                  > )
                  >
                  > Note the use of ( ) where the original had """ """. Also note that
                  > each line has quotes at start/end (the first has ' to avoid escaping
                  > text/css). There are no commas separating each line (and the \n is still
                  > for formatting). Using the ( ) creates an expression, and Python is nice
                  > enough to let one split expressions inside () or[lists], {dicts}, over
                  > multiple lines (I used that feature in a few spots to put call arguments
                  > on multiple lines). Two strings that are next to each other
                  >
                  > "string1" "string2"
                  >
                  > are parsed as one string
                  >
                  > "string1string2 "
                  >
                  > Using """ (or ''') is the cleanest of those choices, especially if
                  > you want to do preformatted layout of the text. It works similar to the
                  > Ruby/PERL construct that basically said: Copy all text up to the next
                  > occurrence of MARKER_STRING.[/color]

                  Thank you for your explanation, now it makes sense.

                  Brian

                  Comment

                  Working...