[英]trim string from index
I have now a more or less working example, as you can see below, to kill some pods in openshift. 正如下面您所看到的,我现在有一个或多或少的工作示例,可以在openshift中杀死一些吊舱。
It's now stuck at this point. 现在它停留在这一点上。
myindx = out.find(key)
print out[myindx:-1].rstrip()
out
is the output of oc get pod --all-namespaces ...
which looks like this out
是oc get pod --all-namespaces ...
的输出oc get pod --all-namespaces ...
看起来像这样
-n namespace01 pod-name-9-xd45
-n namespace01 pod-name-9-xd67
-n namespace02 pod-name2-9-xd45
-n namespace02 pod-name2-9-rd45
with out.find(key)
I find the right line now I need to get the whole line to call oc delete $line
. 使用
out.find(key)
我现在找到正确的行,我需要获得整行来调用oc delete $line
。
In perl I would put all output from oc get pod --all-namespaces ...
into an array and search in every line in the array but how can I do this in python? 在perl中,我会将
oc get pod --all-namespaces ...
所有输出放入数组中,并在数组的每一行中进行搜索,但是如何在python中做到这一点呢?
Thanks for any help. 谢谢你的帮助。
#!python
import StringIO
import sys
import re
import subprocess
import shlex
from collections import defaultdict
# Since you're keeping counts, we'll initialize this so that the values
# of the dictionary are `int`
myhash = defaultdict(lambda: 0)
if sys.argv[3] != "NodeReady":
sys.exit(0)
commandline = shlex.split("/bin/oc get pod --all-namespaces")
proc = subprocess.Popen(commandline,stdout=subprocess.PIPE)
out, err = proc.communicate()
buf = StringIO.StringIO(out)
for line in buf.readlines():
# print line
"""
Diese Projekte und pod Zeilen werden ignoriert
"""
if re.match('^NAMESPACE|.*-router|^logging|^default|.*infra-ipfailover|.*-(build|deploy)',line):
continue
linesplited = line.split()
prefix = re.split('(.*)-\d+-\w+$',linesplited[1])
#print linesplited[1]
#print prefix[1]
myhash[prefix[1]] += 1
commandline = shlex.split('/bin/oc get pod --all-namespaces -o template --template=\'{{range .items }} -n {{ .metadata.namespace }} {{.metadata.name}}\n{{end}}\'')
proc = subprocess.Popen(commandline,stdout=subprocess.PIPE)
out, err = proc.communicate()
#print out
buf = StringIO.StringIO(out)
#print buf
#for line in buf.readlines():
# print line
for key in myhash.keys():
print "<<<>>>"
# print myhash[key] % 2 , "\n"
print key,":",myhash[key]
if myhash[key] % 2 :
if myhash[key] > 1:
print "mod 2",key,":",myhash[key]
myindx = out.find(key)
print out[myindx].rstrip()
else:
print "not mod 2",key,":",myhash[key]
print out.find(key)
I'm shooting a little in the dark here but I think your problem can be reduced to the following. 我在这里在黑暗中开枪,但我认为您的问题可以减少到下面的程度。
After this line, out
should have all the output you mentioned. 在此行之后,
out
应该具有您提到的所有输出。
out, err = proc.communicate()
out
probably looks like this: out
大概是这样的:
-n namespace01 pod-name-9-xd45\n
-n namespace01 pod-name-9-xd67\n
-n namespace02 pod-name2-9-xd45\n
-n namespace02 pod-name2-9-rd45\n
Rather than shove it into a StringIO
object, you can cut up this string into an array: 您可以将此字符串切成数组,而不是将其推到
StringIO
对象中:
output_lines = [x.strip() for x in output.split('\n')]
And then output_lines
looks like this: 然后
output_lines
看起来像这样:
['-n namespace01 pod-name-9-xd45',
'-n namespace01 pod-name-9-xd67',
'-n namespace02 pod-name2-9-xd45',
'-n namespace02 pod-name2-9-rd45']
And then you want to find every the string in the array that contains your key. 然后,您要查找包含键的数组中的每个字符串。
if myhash[key] % 2 :
if myhash[key] > 1:
print "mod 2",key,":",myhash[key]
for ol in output_lines:
myindx = ol.find(key)
if myindx > -1:
print ol
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.