Help Required

Bengt Richter bokr at oz.net
Mon Mar 18 17:21:18 EST 2002


On 18 Mar 2002 09:55:18 -0800, surajsub at netscape.net (Surajsub) wrote:

>Chris Liechti <cliechti at gmx.net> wrote in message news:<Xns91D31A8FD3696cliechtigmxnet at 62.2.16.82>...
>> surajsub at netscape.net (Surajsub) wrote in 
>> news:cf4a8ef1.0203151724.440b67ac at posting.google.com:
>> > Hi,
>> > I am a newbie to Python and am pretty fascinated by it power and
>> > speed.
>> 
>> Welcome
>> 
>> >I am however puzzled in trying to write this script.
>> > The script builds a list of userids to be fed to ldap and it is
>> > supposed to be
>> > 
>> > uid1,uid2,uid3....and so on.( Notice there is no space)
>> > 
>> > However when i am trying to create the uid list it gives
>> > 
>> > uid1, uid2, uid3,...and so on.
>> > how do i get rid of this leading space if u would call it..
>> > string.lstrip does not work either.
>> 
>> i don't see what you're doing. please post some example code alomg 
>> with such questions. however some guesses:
>> 
>> if you have a list of numbers: n = [1,2,3,4]
>> you can make string of it using: ','.join(map(str,n))
>> 
>> or are you parsing the numbers from a string? then you could try:
>> >>> import string
>> >>> n = map(string.strip, '1, 2, 3, 4'.split(','))
>> 
>> and if want numbers instead of strings in the list:
>> >>> n = map(int, '1, 2, 3, 4'.split(','))
>> 
>> (*) "map" is simply calling the 1st argument (a function) with each 
>> element of the list 2nd arg and saves the result in a list which it 
>> returns.
>> you can achieve the same with a "for" loop over the list and saving 
>> the result in an other list, but i find the "map" is very elegant.
>> 
>> chris
>
>Ok here is the code..I just need a set of uid's and other similar
>parameters which i need to feed to ldap.these are just a dummy set of
>values.
>=============================================================
>#!/usr/local/bin/python
>import string
>
>MIN=0
>MAX=25
>UID="UID"
>val=""
There is no need to bind val to "" here. You are not reserving space for a value
when you do this in Python. Python takes care of space for you. val="" just tells
Python to make a linkage between the name val and the constant "" so you can use
val later to look up the value you bound it to.
>for x in range(MIN,MAX,1):
In this case, your range expression could be written range(MAX) because of the defaults
of zero start and one for step. However, since you have defined a symbol for the start
value, it should probably be carried through as range(MIN, MAX).
>    if( x == 0 ):
>        x=str(x)
>        val=UID,x+','
>        st=string.join(val,"")
>        st=string.strip(st)
That sure is working hard to accomplish (BTW, why the strip?)
         st = "%s%d," % (UID,x)
>        print st,
>    elif(x > 0 ):
>        x=str(x)
>        val='UID'+x+',',
Why the comma here -----^  ? Just to make the next line necessary? 
>        st=string.replace(string.join(val,""),' ','')
I urge you to play more at the command line. Notice:
 >>> x='0'
 >>> val='UID'+x+',',
 >>> val
 ('UID0,',)

now leaving off the comma:
 >>> val='UID'+x+','
 >>> val
 'UID0,'

which is what you wanted, right? ;-)
>        print st,
>==========================================================
>
>The output that this script produces is 
>
>UID0, UID1, UID2, UID3, UID4, UID5, UID6, UID7, UID8, UID9, UID10,
>UID11, UID12, UID13, UID14, UID15, UID16, UID17, UID18, UID19, UID20,
>UID21, UID22, UID23, UID24,
>
>
>Notice the spaces between the , and the UID
>============================================
>I need to get rid of these spaces.How do i do it..
>
>Thanks

The spaces come from the print statements, not the data. The solution is a lot
easier than your code ;-)

For an explanation of print, please read

    http://www.python.org/doc/ref/print.html

Print prefixes a space in front of its output unless it is at the beginning of a line,
or some special condition applies (like something else having written to stdout).

Most of the time, this works out fine, but when it doesn't there are several ways to
do what you want.

1) Join your output into a line before printing, e.g., using your parameters:
 >>> MIN=0
 >>> MAX=25
 >>> UID="UID"
 >>> print ','.join([UID+str(i) for i in xrange(MIN,MAX)])
 UID0,UID1,UID2,UID3,UID4,UID5,UID6,UID7,UID8,UID9,UID10,UID11,UID12,UID13,UID14,UID15,UID1
 6,UID17,UID18,UID19,UID20,UID21,UID22,UID23,UID24

(The output is a single line that wrapped).

2) You could make yourself a printf function that works very much like C, with no automatic
spaces or newlines (leaving out return value for less interactive confusion):

def printf(fmt, *args):
    import sys
    sys.stdout.write(fmt % args)

Then you can use it like:
 >>> for i in xrange(MIN,MAX):
 ...     if i == 0:
 ...         printf('%s%d',UID,i)
 ...     else:
 ...         printf(',%s%d',UID,i)
 ...
 UID0,UID1,UID2,UID3,UID4,UID5,UID6,UID7,UID8,UID9,UID10,UID11,UID12,UID13,UID14,UID15,UID1
 6,UID17,UID18,UID19,UID20,UID21,UID22,UID23,UID24>>>

(Note that the >>> prompt came without a newline before it, so we might want to add a plain
print after the for loop is done).

3) We could directly manipulate sys.stdout.softspace to kill the space prefixing that
results from the comma at the end of the print lines:

 >>> for i in xrange(MIN,MAX):
 ...     if i == 0:
 ...         print '%s%d' % (UID,i),
 ...     else:
 ...         print ',%s%d' % (UID,i),
 ...     sys.stdout.softspace = 0
 ...
 UID0,UID1,UID2,UID3,UID4,UID5,UID6,UID7,UID8,UID9,UID10,UID11,UID12,UID13,UID14,UID15,UID1
 6,UID17,UID18,UID19,UID20,UID21,UID22,UID23,UID24>>>

I'm not sure softspace was intended to be used that way.

 You could make the leading comma conditional in the format various ways too, e.g. using printf():
 >>> for i in xrange(MIN,MAX):
 ...     printf('%s%s%d', i and ',' or '', UID, i)
 ...
 UID0,UID1,UID2,UID3,UID4,UID5,UID6,UID7,UID8,UID9,UID10,UID11,UID12,UID13,UID14,UID15,UID1
 6,UID17,UID18,UID19,UID20,UID21,UID22,UID23,UID24>>>

(The expression i and ',' or '' results in '' if i==0 and ',' otherwise).

HTH,

Regards,
Bengt Richter

<PS. to dev gurus>
Why is a de-dented-to-zero line not accepted interactively?
Couldn't it postpone execution until a later blank line,
and not demand an immediate one, once in the mode of accepting
multiple lines? It would be handy for showing an example like
the above with a single print at the end.
</PS. to dev gurus>




More information about the Python-list mailing list