[Tutor] printing columns
Chris Kassopulo
ckasso@sprynet.com
Thu, 25 Jul 2002 22:27:21 -0400
On Thu, 25 Jul 2002 19:46:39 -0500
"Rob" <rob@uselesspython.com> wrote:
> Actually, what I had in mind was to have output I could paste directly
> into a C++ header file. The output from my original code should
> compile without error, but I thought it would be a nice touch to
> display it in pretty columns.
>
> Rob
>
Greetings,
This is my first post to the list. I'm new to python and my programming
experience is very limited (poking around in fortran and c 15 years
ago).
By coincidence, the first python script I decided to write is a program
to output a list in columns, so I'll throw my hat in the ring. Already,
my script has benefitted from this thread. Here is my solution to the
script at hand:
#! /usr/bin/env python
import string
import sys
menuItems = (sys.builtin_module_names)
# find longest string
i = 0
longestLength = len(menuItems[i])
while i < len(menuItems):
x = len(menuItems[i])
i = i + 1
if x > longestLength:
longestLength = x
# field width for ljust
fieldWidth = longestLength + 2
# output the list
myInt = 1
numberofColumns = 1
currentColumn = 1
for item in menuItems:
if currentColumn < numberofColumns:
print '#define ROB_' + string.ljust(item,fieldWidth) +
str(myInt),
currentColumn = currentColumn + 1
if myInt == len(menuItems):
print
else:
print '#define ROB_' + string.ljust(item,fieldWidth) +
str(myInt)
currentColumn = 1
myInt = myInt + 1
--
Chris Kassopulo _/\_ Linux User #199893 _/\_ Slackware