Mohan
Mohan

Reputation: 59

How to search for the word in each line of a file and print the next value of it in python

I want to search for the word in the file and print next value of it using any way using python

Following is the code :

def matchTest(testsuite, testList):

  hashfile = open("/auto/file.txt", 'a')

  with open (testsuite, 'r') as suite:
      for line in suite:
          remove_comment=line.split('#')[0]
          for test in testList:
              if re.search(test, remove_comment, re.IGNORECASE):
                  hashfile.writelines(remove_comment)
                  search_word=remove_comment.split(':component=>"', maxsplit=1)[-1].split(maxsplit=1)
                  print(search_word)
  hashfile.close()

remove_comment has the following lines:

{:component=>"Cloud Tier Mgmt", :script=>"b.py", :testname=>"c", --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml"}

{:skipfilesyscheck=>1, :component=>"Content Store", :script=>"b.py", --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml -s"}

{:script=>"b.py", :params=>"--ddrs=$DDRS --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml", :numddr=>1, :timeout=>10000, :component=>"Cloud-Connectivity" }

So now I want the output to be only the compnent value as following:

Cloud Tier Mgmt

Content Store

Cloud-Connectivity

Please anyone help

Upvotes: 1

Views: 102

Answers (5)

egemenakturk
egemenakturk

Reputation: 375

Try this function

def get_component(file_path):
word_list = []
file = open(file_path, 'r')
i = 0;
for line in file:
    for word in line.split(','):
        word = word[1:]
        if word.startswith(":component=>"):
            word = word[13:-1]
            word_list.append(word)
            print(word)
            return word_list

You can copy-paste and give file_path it going to work well.

here is my output:

['Cloud Tier Mgmt', 'Content Store', 'Cloud-Connectivity ']

Upvotes: 2

Booboo
Booboo

Reputation: 44013

Assuming the rest of the code is correct (we can't see what regular expressions you are using), you need to change only one line to:

search_word = remove_comment.split(':component=>"', maxsplit=1)[-1].split('"', maxsplit=1)[0]

Upvotes: 2

Leo Arad
Leo Arad

Reputation: 4472

You can try

def matchTest(testsuite, testList):
    hashfile = open("/auto/file.txt", 'a')

    with open (testsuite, 'r') as suite:
      for line in suite.split("\n"):
        remove_comment=line.split('#')[0]
        for i in remove_comment.split(","):
            if "component=>" in i:
                search_word = re.search("\"(.*)\"", i).group(1)
                print(search_word)
    hashfile.close()

Output

Cloud Tier Mgmt
Content Store
Cloud-Connectivity

Upvotes: 1

Ronald
Ronald

Reputation: 3305

def matchTest(testsuite, testList):
    hashfile = open("hash.txt", 'a')
    with open ('text.txt', 'r') as suite:
        lines = [line.strip() for line in suite.readlines() if line.strip()]
        print(lines)
        for line in lines:
            f = re.search(r'component=>"(.*?)"', line)  # find part you need
            hashfile.write(f.group(1)+'\n')
    hashfile.close()

Output written to file:

Cloud Tier Mgmt
Content Store
Cloud-Connectivity

Upvotes: 1

deadshot
deadshot

Reputation: 9051

Try this:

import re

remove_comment = '''{:component=>"Cloud Tier Mgmt", :script=>"b.py", :testname=>"c", --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml"}

{:skipfilesyscheck=>1, :component=>"Content Store", :script=>"b.py", --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml -s"}

{:script=>"b.py", :params=>"--ddrs=$DDRS --clients=$LOAD_CLIENT --log_level=DEBUG --config_file=a.yaml", :numddr=>1, :timeout=>10000, :component=>"Cloud-Connectivity" }'''

data = [x.split('"')[1] for x in re.findall(r'component=>[^,:}]*', remove_comment)]
print(data)

Output:

['Cloud Tier Mgmt', 'Content Store', 'Cloud-Connectivity']

Upvotes: 1

Related Questions