python adds an extra half space when reading froma string or list -- back to the question

alex23 wuwei23 at gmail.com
Mon Jul 1 22:37:08 EDT 2013


On 2/07/2013 5:32 AM, Joel Goldstick wrote:
> I copied the original question so that the rant on the other
 > thread can continue.  Let's keep this thread ontopic

You've included the same set of code twice. Also, it doesn't run as is,
so you haven't reduced it to a minimal working example for us to test.

Python's print adds a space where there's a comma, try this at the
interactive prompt:

 >>> print 'a','b'
a b

I don't think you're taking this into account. There are quite a few 
other problems with your code, though:

> number_drawn=()
> def load(lot_number,number_drawn):

You've assigned `number_drawn` to a tuple, which is an immutable type. 
Within the `load` function you then do:

 >              number_drawn=raw_input("number: ")

Which just re-assigns `number_drawn` to the output of the `raw_input`
function _within the function itself_. The value isn't available
outside of the function, if that is what you're intending. You also
haven't defined `lot_number` in your code, and again you re-assign it
within the body of the `load` function:

 >      for lot_number in range(first,last):

Which has no impact on any global definition. Since you're not actually
passing values into the function you can probably do without both
arguments and just go with:

     def load():

 >      first=input("enter first lot: ")
 >      last=input("enter last lot: ")

You should never use `input`, it evaluates the expression entered.
Always use `raw_input` unless you absolutely know what you're doing.

 >      for lot_number in range(first,last):

`range` will start at `first` and finish _at but not including `last`. 
If you want 4 lines when you enter first=1 and last=4, then you need to 
increment `last` by 1:

         for lot_number in range(first,last+1):

>          finale_line.append(line_out)

Where is `finale_line` defined? You haven't shown it but my guess is 
you've made it an empty list. You're also not returning anything from 
the function, which implies you're relying on global scope to hold the 
result. This is bad practice. You should start your function with:

     finale_line = []

And then end it with:

     return finale_line

Which would allow you to replace:

> finale_line2=finale_line
> load(lot_number,number_drawn)

With:

     finale_line = load()

> for a in finale_line:
>      print"\n",
>      print a[0]," ",
>      space_count=1
>      for b in range(1,5):

This condition will _always_ be true:

>          if int(a[b])<10:

Because of the way you're stepping through `finale_line`, you're only 
ever looking at a single character, so that value will always be from 0
to 9. You clearly want to allow for double digit numbers, so not storing 
them all as a big string would be a good start. You're dealing
with numbers, so hold them as numbers in a list.

Here's a more flexible approach that I think does what you want:

     import os

     def load_numbers():
         first=int(raw_input("enter first lot: "))
         last=int(raw_input("enter last lot: "))
         finale_line = []
         for lot_number in range(first,last+1):
             line_out = []
             for count in range(1,5):
                 number_drawn=raw_input("lot %d, number %d: " %
                     (lot_number, count))
                 line_out.append(number_drawn)
             finale_line.append((lot_number, line_out))
         return finale_line

     finale_line = load_numbers()

     # get space needed from the maximum number entered
     largest_number = max(
         number for row,numbers in finale_line for number in numbers)
     space_count = len(str(largest_number))

     # get no. of columns from the first set of numbers
     columns = len(finale_line[0][1])
     # space to cover the lot numbers
     print ' ',
     # print the columns
     for column in xrange(1, columns+1):
         spacing = space_count - len(str(column))
         print '%s%d' % (' '*spacing, column),

     for lot, numbers in finale_line:
         print os.linesep, # use the EOL used by the current
                           # operating system
         print lot,
         for number in numbers:
             spacing = space_count - len(str(number))
             print '%s%d' % (' '*spacing, number),

However, if you want to make your life a _lot_ easier when printing
tabular data, I highly recommend using a library dedicated to just
that, something like:

https://code.google.com/p/prettytable/




More information about the Python-list mailing list