-3

I have a big file with some format. I want to substitute some items in the string by others, the string is long (like a big XML but with no format).

I know where they are, i could locate them by using a regular expression for each, but i wonder which is the best method, easier and better if its the most efficient way.

pavel
  • 26,538
  • 10
  • 45
  • 61
Darkgaze
  • 2,280
  • 6
  • 36
  • 59
  • possible duplicate of [Ignore str.format(\*\*foo) if key doesn't exist in foo](http://stackoverflow.com/questions/28094590/ignore-str-formatfoo-if-key-doesnt-exist-in-foo) – Nir Alfasi Feb 11 '15 at 07:21
  • 1
    @alfasin: I cannot understand how the linked question is a duplicate of this. – Abhijit Feb 11 '15 at 07:23
  • @Abhijit same idea, want to be able to substitute string items using a kind of template. The answer I gave there is probably what the OP here wants as well. – Nir Alfasi Feb 11 '15 at 07:25
  • The name of the question is not as clear as mine. – Darkgaze Feb 11 '15 at 08:33

1 Answers1

1

format/% already searches the string for parameter placeholders internally. Since they're implemented in C, you're not gonna beat their performance with Python code even if your search and replace workload is somewhat simpler. See Faster alternatives to numpy.argmax/argmin which is slow for a glance on C to Python relative performance.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152