Death420

escape special chars python

Jul 24th, 2022
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import os, datetime as dt
  2. ADD_BENCHMARK_TO_LOG_FILE = True
  3. def GetMessageContent():
  4.     result = ''
  5.     with open("message.txt") as file:      
  6.         result = file.read()       
  7.     return result
  8.  
  9. map = {
  10.         '\\' : "\\\\", '`' : "\\`",
  11.         '*' : "\\*", '_' : "\\_"
  12. }
  13. content = GetMessageContent()
  14. t1 = dt.datetime.now()
  15. result = []
  16.  
  17. for c in content:
  18.     dictResult = map.get(c, None)
  19.     if dictResult != None:
  20.         result.append(dictResult)
  21.     else:
  22.         result.append(c)
  23.        
  24. resultStr = ''.join(result)
  25. t2 = dt.datetime.now()
  26. print(f'That took {(t2 - t1).microseconds * 1000} nanoseconds.\n')
  27.  
  28. if ADD_BENCHMARK_TO_LOG_FILE:
  29.     with open("log.txt", "a") as file:
  30.         file.write(f'Python: {(t2 - t1).microseconds * 1000} nanoseconds.\n')
  31.  
  32. if input("See resulting text? Y or N?\n").upper() == 'Y':
  33.     print(resultStr)
  34.  
Advertisement
Add Comment
Please, Sign In to add comment