如何从字符串末尾去掉逗号?我试过了
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(",")
也尝试过t = temp.rstrip(",")
,但都不起作用。
这是代码:
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()
这是输出:
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'
删除字符串末尾的所有逗号:
呃,那尊者呢:
再想一想,
rstrip
本身应该工作得很好,所以从awk
得到的字符串有点不符合您的期望。我们得看看。我怀疑是因为你的字符串没有以逗号结尾。当你跑步时:
输出为:
换言之,如果字符串末尾有一个换行符和一个逗号,则需要同时使用
rstrip
和",\n"
两个字符。好吧,根据你的评论,以下是你正在尝试的:
实际上,从两个
print
语句中获取的是什么?日志文件中附加了什么?我特别的
uptime
没有<a$11
:但你的可能不同。
不过,我们还需要查看脚本的输出。
当你说
然后,awk进程的输出被打印到stdout(例如终端)。
awk_stdout
设置为None
。awk_stdout.rstrip('\n')
引发一个AttributeError
,因为None
没有名为rstrip
的属性。当你说
然后不向stdout打印任何内容(例如终端),并且
awk_stdout
将awk
命令的输出作为字符串。相关问题 更多 >
编程相关推荐