Click here to Skip to main content
15,887,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file which contains below lines.

Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)

Here i need to find only GUID values.

Ex:381b4222-f694-41f0-9685-ff5bb260df2e
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
a1841308-3541-4fab-bc81-f71556f20b4a

How to do this?

I am beginner to python.

What I have tried:

I tried below thing

testfile = os.path.join(os.getcwd(), "power_all.txt")
with open(testfile, "r+") as pwr:
   for line in pwr:
       scheme_guid = line.split("GUID:")[-1]
       print(scheme_guid)


I getting output as below:

Existing Power Schemes (* Active)

-----------------------------------

381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)

8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)

a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) *
Posted
Updated 10-Apr-20 20:36pm
v2
Comments
Patrice T 11-Apr-20 1:58am    
What is the problem with output ?
Member 8724221 11-Apr-20 2:06am    
I need only values.
Ex: 381b4222-f694-41f0-9685-ff5bb260df2e
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
a1841308-3541-4fab-bc81-f71556f20b4a
Patrice T 11-Apr-20 2:10am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 11-Apr-20 3:28am    
You just need to do a second split using the space as delimiter.

1 solution

Quote:
How to find particular string in file which is dynamic in nature using Python

You need to learn Regular Expressions (RegEx)
Basically, RegEx have been create to match patterns, which is exactly what you need.
Different approach are possible, but this RegEx "GUID: (.*) \(" will match the GUID.
-----
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900