-1

I am a new python User. How can I print in python for example only Test12 from this file:

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"

I tried with:

split = re.split('\n |:',examplefile)
print(split[2])
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Topi
  • 1
  • 2
    wait now. where does Test12 come from? Test1+Test2? – Anton vBR Sep 27 '18 at 15:03
  • Hi Topi! welcome to Stack Overflow. In order to help you, we need to see the code that you've written, so we can find the your flaw. Also, please clearly describe the significant variables and functions and what you intent to get from the output. – progyammer Sep 27 '18 at 15:13
  • Welcome to the site. Please expand on details in your question and make it clear what your errors are, and what you expect to happen. https://stackoverflow.com/help/how-to-ask – Adam B Sep 27 '18 at 15:13

1 Answers1

0

To answer your question you need the following:

  • Knowledge how to read txt files
  • Knowledge about regex

And here is a possible answer:

with open(examplefile) as f:
    t = f.read()

m = re.search(r"Cluster : (.+?)\n", t)
if m:
    print(m.group(1))

This will print TEST2

Anton vBR
  • 15,331
  • 3
  • 31
  • 42