Strange tab problem!

Discussion in 'Python' started by tjland@iserv.net, Aug 25, 2003.

  1. Guest

    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.keys():
    print "Book: ",x," \tAuthor: ",title_author[x],"
    \tCopyright:",title_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!
     
    , Aug 25, 2003
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > for x in title_author.keys():
    > print "Book: ",x," \tAuthor: ",title_author[x],"
    > \tCopyright:",title_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.


    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(book title):SSTAuthor: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" -> "12345678SSSSSSSSb"

    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.keys()))
    authorWidth = max(map(len, title_author.values()))
    copyrightWidth = max(map(lambda y: len(str(y)), title_copyright.values()))

    for title in title_author.keys():
    ptitle = title.ljust(titleWidth)
    pauthor = title_author[title].ljust(authorWidth)
    pcopyright = str(title_copyright[title]).rjust(copyrightWidth)

    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
     
    Peter Otten, Aug 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hermit Dave
    Replies:
    0
    Views:
    440
    Hermit Dave
    Jan 22, 2004
  2. News Sympatico
    Replies:
    0
    Views:
    311
    News Sympatico
    May 21, 2005
  3. tab 2 into tab 4 ?

    , Jun 19, 2005, in forum: Python
    Replies:
    2
    Views:
    325
  4. sfp
    Replies:
    1
    Views:
    490
    mrcakey \(The Eclectic Electric\)
    Feb 5, 2007
  5. max
    Replies:
    2
    Views:
    376
    Lambik
    Apr 1, 2007
Loading...

Share This Page