string replace shortcut

V

vedrandekovic

Hello,

I am trying to replace some string with list objects:

# functions in module " my_module_with_functions_1 ":

my_func1 it's value "function1"

my_func2 it's value "function2"

# functions in module " my_module_with_functions_2 ":

my_func100 it's value "bla bla 1"

my_func200 it's value "bla bla 2"

.........now, we need find and replace functions from module "
my_module_with_functions_1 " and
replace them with functions from module " my_module_with_functions_2
"

with: my_text1.replace(items1,items2)

Result must be:
bla bla 1 bla bla 2


Regards,
Vedran
 
G

Gabriel Genellina

Hello,

I am trying to replace some string with list objects:


# functions in module " my_module_with_functions_1 ":
my_func1 it's value "function1"
my_func2 it's value "function2"

# functions in module " my_module_with_functions_2 ":
my_func100 it's value "bla bla 1"
my_func200 it's value "bla bla 2"

........now, we need find and replace functions from module "
my_module_with_functions_1 " and
replace them with functions from module " my_module_with_functions_2
"

with: my_text1.replace(items1,items2)

Result must be:

bla bla 1 bla bla 2

I'm unsure what you want. You keep saying "functions" but they are
apparently strings.
If your problem is how to replace many strings, just do it one at a time
(asuuming they're unique so the order is not important).

def multi_replace(text, list1, list2):
for rep1, rep2 in itertools.izip(list1, list2):
text = text.replace(rep1, rep2)
return text

py> multi_replace("a sentence with a few words", ["sentence","words"],
["man","d
ollars"])
'a man with a few dollars'

Now, your problem may be building both lists. They must be syncronized,
that is, the first element on one list must correspond to the first
element on the other, and so on. The import statement doesn't guarantee
any ordering so I think the best way would be to define an __all__
attribute on both modules, and import that:

from my_module_with_functions_1 import __all__ as list_of_names_1
from my_module_with_functions_2 import __all__ as list_of_names_2

new_text = multi_replace(my_text1, list_of_names_1, list_of_names_2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top