TSV to HTML
Brian
bnblazer at gmail.com
Thu Jun 1 18:27:22 EDT 2006
Dennis Lee Bieber wrote:
> On 1 Jun 2006 03:29:35 -0700, "Brian" <bnblazer at gmail.com> declaimed the
> following in comp.lang.python:
>
> > 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.
> >
> 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)
>
> > One question (and this is a topic that I still have trouble getting my
> > arms around). Why is the text in STYLEBLOCK tripple quoted?
> >
> 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:#ffffe0;\n\
> font-weight: bold;\n\
> text-align: center; }\n\
> .cell0 { background:#3ff1f1; }\n\
> .cell1 { background:#f8f8f8; }\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:#ffffe0;\n"
> " font-weight: bold;\n"
> " text-align: center; }\n"
> ".cell0 { background:#3ff1f1; }\n"
> ".cell1 { background:#f8f8f8; }\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.
Thank you for your explanation, now it makes sense.
Brian
More information about the Python-list
mailing list