How to call dynamicly string operations in Python?
Question:
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
Answers:
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'