Strange tab problem!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tjland@iserv.net

    Strange tab problem!

    Okay i wrote a small program that allows users to creat book listings
    featuring copyright date author and title. Now when i display the book
    listings i get this.

    Book: testover6 Author: ??? Copyright: 2003
    Book: testu6 Author: ??? Copyright: 2003

    Okay now after the book title their is supposed to a tab the coding goes
    like this.

    for x in title_author.ke ys():
    print "Book: ",x," \tAuthor: ",title_aut hor[x],"
    \tCopyright:",t itle_copyright[x]

    it seems that whenever i go over six characters it adds another tab. Is
    there a preset field limit to six bytes. What is happening? Thanx in
    advance.


    When you see the net take the shot
    When you miss your shot shoot again
    When you make your shot you win!

    Just remember
    Offense sells tickets
    But defense wins championships!

  • Peter Otten

    #2
    Re: Strange tab problem!

    tjland@iserv.ne t wrote:
    [color=blue]
    > for x in title_author.ke ys():
    > print "Book: ",x," \tAuthor: ",title_aut hor[x],"
    > \tCopyright:",t itle_copyright[x]
    >
    > it seems that whenever i go over six characters it adds another tab. Is
    > there a preset field limit to six bytes. What is happening? Thanx in
    > advance.[/color]

    Note that Python inserts a space between the arguments of a print statement.
    So you *always* get (replacing space with "S" and tab with "T")

    "Book:SSxSS(boo k title):SSTAutho r:SS(author name)TCopyright :S(year)"
    123456789a(book title)123

    How this is displayed, depends on your editor settings. Assuming 1 tab == 8
    spaces:

    "Tb" -> "SSSSSSSSb"
    "1Tb" -> "1SSSSSSSb"
    "12Tb" -> "12SSSSSSb"
    ....
    "1234567Tb" -> "1234567Sb"
    "12345678Tb " -> "12345678SSSSSS SSb"

    So every time you reach the 8 character limit, another tab appears to be
    (but is not!) inserted. Solution:

    (1) do not use tabs, and
    (2) calculate column widths before printing

    title_author = {"For whom the bell tolls": "Hemingway" ,
    "Zauberberg ": "Mann"}
    title_copyright = {"For whom the bell tolls": 1234,
    "Zauberberg ": 123}

    titleWidth = max(map(len, title_author.ke ys()))
    authorWidth = max(map(len, title_author.va lues()))
    copyrightWidth = max(map(lambda y: len(str(y)), title_copyright .values()))

    for title in title_author.ke ys():
    ptitle = title.ljust(tit leWidth)
    pauthor = title_author[title].ljust(authorWi dth)
    pcopyright = str(title_copyr ight[title]).rjust(copyrig htWidth)

    print "Book:", ptitle, "Author:", pauthor, "Copyright" , pcopyright

    The generalization for arbitrary tables (and finding out the samples' years
    of publication) is left as an exercise to the OP :-)

    Peter

    Comment

    Working...