Advertisement
gubichas

7.1

Dec 20th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. #7.1
  2. import re
  3. def replace(filePath, text, subs, flags=0):
  4.     with open(file_path, "r+") as file:
  5.         # read the file contents
  6.         file_contents = file.read()
  7.         text_pattern = re.compile(re.escape(text), flags)
  8.         file_contents = text_pattern.sub(subs, file_contents)
  9.         file.seek(0)
  10.         file.truncate()
  11.         file.write(file_contents)
  12. f = open('/Users/alexsandrgubich/Downloads/кондуит 2022 - Преподаватели и ассистенты (1).tsv','r+')
  13. emails =[]
  14. while True:
  15.     line = f.readline()
  16.     if line=='':
  17.         break
  18.     result = re.search("[^|\w](\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)", line)
  19.     if result!= None:
  20.         emails.append(result.group())
  21.     emails.append('asgubich@edu.hse.ru')
  22.  
  23. f.close()
  24. f = open('/Users/alexsandrgubich/Downloads/кондуит 2022 - Преподаватели и ассистенты.tsv','r+')
  25.  
  26. for email in emails:
  27.     if bool(re.search("@edu.hse.ru|@hse.ru",email) )== True:
  28.  
  29.         #
  30.         emails.pop(emails.index(email))
  31.     else:
  32.         pass
  33. if bool(re.search("@edu.hse.ru|@hse.ru",emails[-1]) )== True:
  34.     emails.pop(emails.index(emails[-1]))
  35. if bool(re.search("@edu.hse.ru|@hse.ru", emails[-2])) == True:
  36.     emails.pop(emails.index(emails[-2]))
  37.  
  38. f.close()
  39.  
  40.  
  41.  
  42.  
  43. file_path = "/Users/alexsandrgubich/Downloads/кондуит 2022 - Преподаватели и ассистенты (1).tsv"
  44. text = ""
  45. subs = "Pr1v@cY REstorED"
  46. for i in emails:
  47.     replace(file_path,i,subs)
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement