Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. # importing the regex module
  2. import re
  3. import csv
  4.  
  5.  
  6. # defining the replace method
  7. def replace(file_path, text, subs, flags=0):
  8.     with open(file_path, "r+", encoding='utf-8') as file:
  9.         # read the file contents
  10.         file_contents = file.read()
  11.         text_pattern = re.compile(re.escape(text), flags)
  12.         file_contents = text_pattern.sub(subs, file_contents)
  13.         file.seek(0)
  14.         file.truncate()
  15.         file.write(file_contents)
  16.  
  17.  
  18. def reformatt_string(input_text):
  19.     return '\"val\": \"'+input_text+'\",'
  20.  
  21.  
  22. file_path = "dialogues-jp.txt"
  23. csv_path = "test.csv"
  24.  
  25. f = open(csv_path,'r', encoding='utf-8')
  26. rdr = csv.reader(f)
  27.  
  28. for findincsv in rdr:
  29.     string_replaced = reformatt_string(findincsv[0])
  30.     new_string = reformatt_string(findincsv[1])
  31.     replace(file_path, string_replaced, new_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement