我是新的python用户。我如何才能在python中从该文件中仅打印例如Test12:
Service details : Request a K8s-Tenant\n\n- Application : TEST1\n- Cluster : TEST2\n- Namspace \"Prod\" :\nRessources values are one-to-one converted to GiB values. (Example: 1 -> 1GiB -> 1024B)\n- Prod - Creation : No\n- \"Prod\" - CPU : 1\n- Prod - RAM : 1\n- \"Prod\" - Storage : 2\n- Namspace \"Test\" :\n\n- \"Test\" - Creation : No\n- \"Test\" - CPU : 1\n- \"Test\" - RAM : 1\n- Storage (Test) : 1\n- Namespace \"Dev\" :\n\n- Deploy (Dev) : No\n- CPU (Dev) : 1\n- RAM (Dev) : 1\n- Storage (Dev) : 1\n- Note :\nTenant resources can only be changed via. change_k8s_tenant"
我尝试过:
split = re.split('\n |:',examplefile)
print(split[2])
答案 0 :(得分:0)
要回答您的问题,您需要以下内容:
这是一个可能的答案:
with open(examplefile) as f:
t = f.read()
m = re.search(r"Cluster : (.+?)\n", t)
if m:
print(m.group(1))
这将打印TEST2