Python Forum
print a word after specific word search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print a word after specific word search
#1
hello all ...
i have this string :

Quote:<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<copyright>Copyright © 2005 - 2016 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<description>en-GB site language</description>
<metadata>
<name>English (en-GB)</name>
<tag>en-GB</tag>
<rtl>0</rtl>
<locale>en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain, uk, united kingdom, united-kingdom</locale>
<firstDay>0</firstDay>
<weekEnd>0,6</weekEnd>
</metadata>
<params />
</metafile>
i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?
Reply
#2
what have you tried?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-22-2019, 06:54 AM)buran Wrote: what have you tried?
match = re.compile(r'"<version>"\:(".*?)["\[]')
a = match.findall(q)
print(a)
i got
Output:
[]
Reply
#4
https://docs.python.org/3.7/library/xml....ttree.html

or use some external module as lxml or BeautifulSoup
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Oct-22-2019, 06:53 AM)evilcode1 Wrote: i have this string :

Quote:<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]
i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?

Hi!

Maybe you could use something like:

import re

string1 = '''<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]'''
requirement1 = re.search('<version>(.*)</version>', string1)
print(requirement1.group(1))
that produces the following output:
Output:
3.6.0
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
(Oct-22-2019, 07:30 AM)newbieAuggie2019 Wrote:
(Oct-22-2019, 06:53 AM)evilcode1 Wrote: i have this string :

i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?

Hi!

Maybe you could use something like:

import re

string1 = '''<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]'''
requirement1 = re.search('<version>(.*)</version>', string1)
print(requirement1.group(1))
that produces the following output:
Output:
3.6.0
All the best,
thank u very much <3 works and this is better than starting coding new code with xml.etree.ElementTree

(Oct-22-2019, 07:05 AM)buran Wrote: https://docs.python.org/3.7/library/xml....ttree.html

or use some external module as lxml or BeautifulSoup

thank u for quick response , but i will prefer regex and @newbieAuggie2019 give me a solution
Reply
#7
(Oct-22-2019, 07:40 AM)evilcode1 Wrote: thank u very much <3 works

You're welcome! Smile
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
why not parse html/xml with regex is discussed so many times... use proper tools for the task or it will bite you when you don't expect
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Oct-22-2019, 07:40 AM)evilcode1 Wrote:
(Oct-22-2019, 07:05 AM)buran Wrote: https://docs.python.org/3.7/library/xml....ttree.html

or use some external module as lxml or BeautifulSoup

thank u for quick response , but i will prefer regex

(Oct-22-2019, 07:50 AM)buran Wrote: why not parse html/xml with regex is discussed so many times... use proper tools for the task or it will bite you when you don't expect

Hi again!

Although I always try to address a problem from the point of view and knowledge of a newbie like myself, if anyone from this site advises not to use what I have suggested in good faith, I would take their advice into consideration, as their knowledge is much much deeper than mine.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Docx Convert Word Header to Body CaptainCsaba 3 354 Jun-02-2021, 01:25 PM
Last Post: Larz60+
  –°ombine (Merge) word documents using python-docx Lancellot 1 2,317 May-12-2021, 11:07 AM
Last Post: toothedsword
  remove vowels in word with conditional ambrozote 12 662 May-02-2021, 06:57 PM
Last Post: perfringo
  How to read check boxes from word document srikanthpython 0 362 Mar-30-2021, 01:58 PM
Last Post: srikanthpython
  looping wordlist keeps repeating when word not found OTH 1 427 Feb-24-2021, 05:36 PM
Last Post: nilamo
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 435 Feb-10-2021, 11:06 AM
Last Post: Larz60+
Question Word, adding a hyperlink from a bookmark using Python mart79 1 448 Jan-14-2021, 08:41 PM
Last Post: Larz60+
  Trying to get the first letter of every word in a list DanielCook 2 512 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  [GoogleTrans] How can i print my translation word ?... JamieVanCadsand 6 7,916 Dec-23-2020, 04:38 PM
Last Post: shyamkumardr
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 2,104 Dec-22-2020, 07:30 AM
Last Post: martabassof

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020