Prostyak

find_replace_method

May 15th, 2022 (edited)
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. def find_replace(script, new_script, name):
  2.     with open(script,encoding="utf-8", mode="r") as script_file:
  3.         script_text = script_file.read()
  4.         for d2mo_script in new_script:
  5.             with open(d2mo_script, encoding='utf-8', mode='r') as d2mo_script_file:
  6.                 text = d2mo_script_file.read()
  7.                 text_index_from = (text.find('"from"')+7)
  8.                 text_index_to = (text.find('"to"')-2)
  9.                 text_old = text[text_index_from:text_index_to]
  10.                 text_index_from = text.find('"to"')+5
  11.                 text_index_to = text.rfind('}')-5
  12.                 text_new = text[text_index_from:text_index_to]
  13.                 if text_old in script_text:
  14.                     script_text = script_text.replace(text_old, text_new)
  15.             with open(name, encoding='utf-8', mode='w') as new_script_file:
  16.                 new_script_file.write(script_text)
Advertisement
Add Comment
Please, Sign In to add comment