开发者

How do I strip the comma from the end of a string in Python?

How do I strip comma from the end of a 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.


This is the code:

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)
开发者_如何学Cawk_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()

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'


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.


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.


Remove all commas at the end of the string:

str = '1234,,,'
str = str.rstrip(',')


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!-).


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(',')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜