Execute f-string in function

Question:

I have a

string = 'long company name with technologies in it'

and want to replace all tokens starting with

search_string ='techno'

with a new token

replace_string = 'tech'.

I wrote a function:

def group_tokens(company_name, string_search, string_replace):   
try:
    x = company_name.split(" ") 
    print(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    exec(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    x = " ".join(x)
    x = " ".join(re.split("s+", x, flags=re.UNICODE))
    return(x)
except:
        return np.nan

If I execute the lines separately it works. But the function itself doesn’t work.

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with technologies in it'

I’d expect

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with tech in it'

How can I "exec" f-string in a function?

Asked By: Judy

||

Answers:

You are overcomplicating this. Simply reassign x:

def group_tokens(company_name, string_search, string_replace):   
  try:
    x = company_name.split(" ") 
    x = [re.sub(f'^{string_search}.*', string_replace, i) for i in x])
    x = " ".join(x)
    x = " ".join(re.split("s+", x, flags=re.UNICODE))
    return x
  except:
    return np.nan

But it’s probably easier to rewrite the function similar to the following:

def group_tokens(company_name, string_search, string_replace):   
  return re.sub(f'b{string_search}S*s*', f'{string_replace} ', company_name, flags=re.UNICODE);
Answered By: knittl
def replace(string,x,y):
    words = string.split(' ')
    string = ''
    while words:
        word = words.pop(0)
        if word.startswith(x):
            word = y
        string+=word+' '
    return string[:-1]

print(replace('long company name with technologies in it', 'techno', 'tech'))

    
Answered By: islam abdelmoumen

I definitely overcomplicated it. Thanks 🙂

def group_tokens(company_name, string_search, string_replace):   
  try:
    x = company_name.split(" ") 
    x = [re.sub(f'^{string_search}.*', string_replace, i) for i in x])
    x = " ".join(x)
    x = " ".join(re.split("s+", x, flags=re.UNICODE))
    return x
  except:
   return np.nan
Answered By: Judy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.