views:

184

answers:

7

how do i strip comma from the end of an string, i tried

awk = subprocess.Popen([r"awk", "{print $10}"], stdin=subprocess.PIPE)
awk_stdin = awk.communicate(uptime_stdout)[0]
print awk_stdin
temp = awk_stdin
t = temp.strip(",")

also tried t = temp.rstrip(","), both don't work.

+3  A: 

Err, how about the venerable:

if len(str) > 0:
    if str[-1:] == ",":
        str = str[:-1]

On second thought, rstrip itself should work fine, so there's something about the string you're getting from awk that's not quite what you expect. We'll need to see that.


I suspect it's because your string doesn't actually end with a comma. When you run:

str = "hello,"
print str.rstrip(",")

str = "hello,\n"
print str.rstrip(",")
print str.rstrip(",\n")

the output is:

hello
hello,

hello

In other words, if there's a newline at the end of the string as well as a comma, you'll need to rstrip both characters with ",\n".


Okay, based on your comment, here's what you're trying:

uptime = subprocess.Popen([r"uptime"], stdout=subprocess.PIPE)
uptime_stdout = uptime.communicate()[0]
print uptime_stdout
awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)
awk_stdin = awk.communicate(uptime_stdout)[0]
print repr(awk_stdin)
temp = awk_stdin
tem = temp.rstrip("\n")
logfile = open('/usr/src/python/uptime.log', 'a')
logfile.write(tem + "\n")
logfile.close()

What are you actually getting from your two print statements and what is being appended to the log file?

My particular uptime doesn't have a $11:

23:43:10 up  5:10,  0 users,  load average: 0.00, 0.00, 0.00

but yours may be different.

Still, we need to see the output of your script.

paxdiablo
surely, you're not suggesting that built-in method stopped working?
SilentGhost
No, but there's info missing from the question. I answered the "how do i strip comma from the end of an string?"
paxdiablo
i get TypeError: unsubscriptable object error
krisdigitx
Well, then I suspect it's not a string...
paxdiablo
uptime = subprocess.Popen([r"uptime"], stdout=subprocess.PIPE)uptime_stdout = uptime.communicate()[0]print uptime_stdoutawk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)awk_stdin = awk.communicate(uptime_stdout)[0]print repr(awk_stdin)temp = awk_stdintem = temp.rstrip("\n")logfile = open('/usr/src/python/uptime.log', 'a')logfile.write(tem + "\n")logfile.close()this is what i am trying to do
krisdigitx
thats the value, just after load average:
krisdigitx
Then surely that would be `$8`, not `$11` - you really need to post the output of your script if you want this solved. It does little good for us to guess at what might be wrong when a little more info will almost certainly pinpoint it.
paxdiablo
A: 

Remove all commas at the end of the string:

str = '1234,,,'
str = str.rstrip(',')
unholysampler
I think you may have meant `str.rstrip(',')`
Will McCutchen
Yeah, I typed too fast. Fixed with the edit.
unholysampler
+1  A: 

I think you'll find that awk_stdin actually ends with a newline (print repr(awk_stdin) to show it clearly), so you'll need to rstrip that away, before rstrip'ping the comma (or, you could do both at once with a RE, but the basic idea is that the comma isn't actually the last character in that string!-).

Alex Martelli
A: 

this is what the code is

uptime = subprocess.Popen([r"uptime"], stdout=subprocess.PIPE)
uptime_stdout = uptime.communicate()[0]
print uptime_stdout

awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)
awk_stdin = awk.communicate(uptime_stdout)[0]
print repr(awk_stdin)
temp = awk_stdin
tem = temp.rstrip("\n")
logfile = open('/usr/src/python/uptime.log', 'a')
logfile.write(tem + "\n")
logfile.close()
krisdigitx
i want to remove the commma, but everytime i use rstrip, it says no attribute rstrip
krisdigitx
+4  A: 

When you say

awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)
awk_stdout = awk.communicate(uptime_stdout)[0]

then the output of the awk process is printed to stdout (e.g. a terminal). awk_stdout is set to None. awk_stdout.rstrip('\n') raises an AttributeError because None has no attribute called rstrip.

When you say

awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE)
awk_stdout = awk.communicate(uptime_stdout)[0]

then nothing is printed to stdout (e.g. the terminal), and awk_stdout gets the output of the awk command as a string.

unutbu
+1, and it's all in docs btw.
SilentGhost
cool, that worked,awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
krisdigitx
@kris, I suggest you accept this answer then. +1 for catching it, ~unutbu.
paxdiablo
A: 

this is the output..

 17:07:32 up 27 days, 37 min,  2 users,  load average: 5.23, 5.09, 4.79

5.23,
None
Traceback (most recent call last):
  File "uptime.py", line 21, in ?
    tem = temp.rstrip("\n")
AttributeError: 'NoneType' object has no attribute 'rstrip'
krisdigitx
read the ~unutbu's answer.
SilentGhost
A: 

If you have whitespace/non printing characters then try something like this:

a_string = 'abcdef,\n'
a_string.strip().rstrip(',') if a_string.strip().endswith(',') else a_string.strip()

saves you the trouble of checking string lengths and figuring out slice indexes.

Of course if you do not need to do anything different for strings that do not end in a comma then you could just do:

a_string.strip().rstrip(',')
Jason Humber