Strange tab problem!

T

tjland

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!
 
P

Peter Otten

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

What had been going on with the list? 0
Sobig in list? 0
Virus in list! 0
Listening to keyboard input? 1
Tkinter? 2
What has been going on? 2
List Virus? 0
TKinter is driving me crazy! 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top