0

I need to find a string "enabled" in an XML file using python script.

I am able to fine a string "enabled" but when i try to find out for a string "enabled" it's not working.

4
  • string is "<logging>enabled</logging>" not enabled Commented Feb 20, 2019 at 6:19
  • how about finding the tags named logging and checking if the value is enabled then? Commented Feb 20, 2019 at 6:20
  • Try to put your code for what you have achieved so far. Commented Feb 20, 2019 at 6:21
  • @Ajay if the answer posted below helped, you may accept it: meta.stackexchange.com/questions/5234/… Commented Mar 5, 2019 at 7:01

1 Answer 1

1

Using BeautifulSoup:

list_test.xml:

<logging>enabled</logging>
<logging>disabled</logging>
<logging>enabled</logging>
<logging>disabled</logging>
<logging>enabled</logging>

and then:

from bs4 import BeautifulSoup

with open('list_test.xml','r') as f:
    soup = BeautifulSoup(f.read(), "html.parser")
    for line in soup.find_all('logging'):
         if line.text == 'enabled':
             print(line.text)

OUTPUT:

enabled
enabled
enabled

EDIT:

To get the complete tags:

Use:

print(line)

Instead of:

print(line.text)
Sign up to request clarification or add additional context in comments.

2 Comments

I need to find out complete string like "<logging>enabled</logging>" not only enabled string.
@Ajay Edited my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.