Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract words from the list which meet certain conditions using python

I am trying to extract words from the list that meet certain conditions. It should read each line and if the line ends with ")" and in that line it should extract words starting from "." and " " space and end to "(".

I know I can’t use the startwith and endwith functions because there are no certain startwith words. That is why I am using re library, but still, my script is not executing.

import re
data = ["int k = b.k(parcel)",
"int k = kon(parcel)",
"int a", 
"int bds",
"obtain.appendFrom(parcel, dataPosition2, readInt2)",
"obtain desFrom(package, dataPosition2, readInt2)",
"int abd(callme)",
"int.dbd(callyou)",
"int throw new UnsupportedOperationException(you)",
"int throw new.UnsupportedOperationException(me)"]

for i in data:
    para = re.findall(r"*[ \.]\s(\w+)\s*[)]" ,i) # start from space and dot and endwith ")"
    i = i.replace(para,"function_call")
    
for i in data:
    print(i)

I want output like :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int k = b.function_call(parcel)
int k = function_call(parcel)
int a 
int bds
obtain.function_call(parcel, dataPosition2, readInt2)
obtain function_call(package, dataPosition2, readInt2)
int function_call(callme)
int.function_call(callyou)
int throw new function_call(you)
int throw new.function_call(me)

>Solution :

Use re.sub to use a regexp to replace a segment. You can’t use the return value of findall as an argument to str.replace in the first place, and doing i = i.replace(...) will not modify the i in the list (since strings are immutable for one).

So, here’s a version that uses a list comprehension to run a regexp replacement on all strings to result in a new list:

import re

data = [
    "int k = b.k(parcel)",
    "int k = kon(parcel)",
    "int a",
    "int bds",
    "obtain.appendFrom(parcel, dataPosition2, readInt2)",
    "obtain desFrom(package, dataPosition2, readInt2)",
    "int abd(callme)",
    "int.dbd(callyou)",
    "int throw new UnsupportedOperationException(you)",
    "int throw new.UnsupportedOperationException(me)",
]

fixed_data = [
    re.sub(r"(\w+)\s*\(", "function_call(", i)
    for i in data
]

for i in fixed_data:
    print(i)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading