Newbie question - leading zeros
I have looked around and cannot seem to find a way to strip leading zeros off of values in a dictionary. Basically, I am looking to do a for loop and any value that has one or more leading zeros would be stripped. Any pointers would be appreciated. Thanks -- Randomly generated signature ICMP: The protocol that goes PING! -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question - leading zeros
On Fri, 13 Oct 2006, Rainy wrote: > > eldorado wrote: >> I have looked around and cannot seem to find a way to strip leading zeros >> off of values in a dictionary. Basically, I am looking to do a for loop >> and any value that has one or more leading zeros would be stripped. Any >> pointers would be appreciated. Thanks >> >> -- >> Randomly generated signature >> ICMP: The protocol that goes PING! > > import string >>>> string.lstrip('0001', '0') > '1' > > Hope this willhelp Perfect. Thank you and everyone else who answered. -- Randomly generated signature u grammar nazis can bite me ass. -- http://mail.python.org/mailman/listinfo/python-list
value exists (newbie question)
Hello, I am trying to parse some files so that if a postal code exists, but is longer than five digits it will return me only the first five digits: ... for insDict in insureDict: insDict['postalcode'] = insDict.get('postalcode')[:5] ... This works, except for when I get a blank postalcode. In which case I get the following error. ERR exceptions.TypeError: iteration over non-sequence What I would like to do is to check to make sure a value exists. I just cannot seem to find the syntax to do that. (not asking anyone to write my code, just looking for a pointer) Thanks -- Randomly generated signature -- In God I Trust -- on all others I use dsniff, ettercap and lczroex -- http://mail.python.org/mailman/listinfo/python-list
getting a process's PID
Hello, I am trying to get python to give me the PID of a process (in this case HUB). I have it working, except for the fact that the output includes \012 (newline). Is there a way to ask python not to give me a newline? Python 1.4 (Oct 14 1997) [C] Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import os >>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'") >>> h = g.readlines() >>> g.close() >>> h ['87334\012'] Thanks in advanced for any guidance. -- Randomly generated signature Whoever said nothing is impossible never tried slamming a revolving door. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting a process's PID
On Wed, 27 Dec 2006, Erik Johnson wrote: > "eldorado" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Hello, >> >> I am trying to get python to give me the PID of a process (in this case >> HUB). I have it working, except for the fact that the output includes >> \012 (newline). Is there a way to ask python not to give me a newline? >> >> Python 1.4 (Oct 14 1997) [C] >> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>>> import os >>>>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'") >>>>> h = g.readlines() >>>>> g.close() >>>>> h >> ['87334\012'] > > > There's more than one way to do it! (Oh, sorry, that's Perl...) > > The two most standard ways would be to call strip() on your string to get > one sans both leading and trialing whitespace > >print h.strip() > > or if you know exactly what you've got (i.e., the newline you don't want is > just the last character), you can just get rid of it: > >h = h[:-1] > Thanks for the help, however it doesnt look like those two solutions quite work: >>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'") >>> h = g.readlines() >>> g.close() >>> h ['87334\012'] >>> h = h[:-1] >>> h [] >>> >>> import string >>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'") >>> h = g.readlines() >>> g.close() >>> print h.strip() File "", line 1 print h.strip() ^ SyntaxError: invalid syntax I looked up the syntax for print and it looks correct (at least to me ;) -- Randomly generated signature You can go anywhere you want if you look serious and carry a clipboard. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting a process's PID
On Wed, 27 Dec 2006, Erik Johnson wrote: > > "eldorado" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>>>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'") >>>>> h = g.readlines() >>>>> g.close() >>>>> h >> ['87334\012'] >>>>> h = h[:-1] >>>>> h >> [] > > Oh, sorry... h is a list here because you are using readlines(). > I am used to doing this: > > fd = os.popen('ps -ef | grep python') > s = fd.read() > > to get a single string. You can do something like > > s = h[0] > > and then operate on s, or you can use read() in place of readlines() to get > h as a single string, or you can operate on the first element of h: > >>>> h = ['87334\012'] >>>> h[0][:-1] > '87334' >>>> h[0].rstrip('\n') > '87334' > > All the error handling to do in the case where you actually have multiple > processes being matched is up to you. ;) Erik, Thank you very much. Works perfect. I am now off to work on the multiple process issue. -- Randomly generated signature Claiming that your operating system is the best in the world because more people use it is like saying McDonalds makes the best food in the world. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting a process's PID
On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote: > eldorado <[EMAIL PROTECTED]> typed > >> Hello, >> >> I am trying to get python to give me the PID of a process (in this >> case >> HUB). I have it working, except for the fact that the output includes >> \012 (newline). Is there a way to ask python not to give me a >> newline? >> >> Python 1.4 (Oct 14 1997) [C] >> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>>> import os >>>>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 >>>>> }'") h = g.readlines() >>>>> g.close() >>>>> h >> ['87334\012'] >> >> Thanks in advanced for any guidance. > > Well, you could do everything in python itself, without using grep and > awk at all: > >>>>> g = os.popen("ps -e -o pid,command") >>>>> for line in g.readlines(): >>>>> if 'HUB' in line: >>>>> pid = line.strip().split(' ')[0] >>>>> break >>>>> print pid > This looks cleaner than the way I was going. I created a file called ps.py #!/usr/local/bin/python import os g = os.popen("ps -e -o pid,command") for line in g.readlines(): if 'HUB' in line: pid = line.strip().split(' ')[0] break print pid When I run ps.py I get the following error. Traceback (innermost last): File "./ps.py", line 5, in ? if 'HUB' in line: TypeError: string member test needs char left operand I googled this error, but wasn't smart enough to figure out exactly what it means. -- Randomly generated signature On the other hand, the early worm gets eaten. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting a process's PID
On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote: > eldorado <[EMAIL PROTECTED]> typed > > Strange!? On my system with Python 2.4 I don't get this error. It is > likely to be a problem of your really ancient python version. Do I > guess correctly from your previous postings, that you're still using > version 1.4?. Sebastian, Yes, I was running this on a box that had 1.4 - I just tested it on a box that has 2.3.5 and it runs perfect. Your changes also allow it to be run on the boxes that still have 1.4 Thanks for all your help. -- Randomly generated signature "I have opinions of my own -- strong opinions --but I don't always agree with them."-G.W.Bush -- http://mail.python.org/mailman/listinfo/python-list