fork download
  1. import re
  2. #My text file:
  3. TEXT_FILE = r"""
  4. the old change is testing.pickup_start
  5. the old model example two is simple.order_facts
  6. the exmple with colons: pickup_drop
  7. the exmple with colons:pickup_drop
  8. model: company1
  9. model: company1
  10. model :company1
  11. model : company1
  12. model:company1
  13. profit
  14. the company1 : company2
  15. old model: company1
  16. the old model is called: company1
  17. the.OTP_bonus: 57
  18. the.order_is_cancelled: Order is Cancelled
  19. track: do.is_taken my name
  20. so: [incentives.total_pay decrease]
  21. testing
  22. testing
  23. field: [orders.order_facts,orders.delivered,orders.total_payout
  24. orders.pickup_start,orders.order_is_cancelled]"""
  25.  
  26. d = {
  27. 'company1':'company2',
  28. 'pickup_start':'pickup_start_at',
  29. 'pickup_drop': 'pickup_drop_at',
  30. 'order_facts':'orders2',
  31. 'OTP_bonus': 'OTP_BONUS_APPLIED',
  32. 'old_model':'new_model',
  33. 'order_is_cancelled':'Order_IS_Cancelled',
  34. 'pm_email':'Pm_email_ID',
  35. 'pickup_start_timestamp':'pickup_start_timestamp_at',
  36. 'is_taken': 'is_taken_y/n',
  37. 'profit':'margin',
  38. 'total_pay':'salary'
  39.  
  40. }
  41.  
  42. pattern = fr"(\.|\bmodel\s?:\s?|:\s?\b(?!company1\b))({'|'.join(d)})\b"
  43. #with open('lookerml2-Copy4.txt','r') as file:
  44. # with open ('new_script.txt','w') as new_code:
  45. # for line in file:
  46. for line in TEXT_FILE.splitlines():
  47. if not line.strip(): continue
  48. changes = re.sub(pattern, lambda x: f'{x.group(1)}{d[x.group(2)]}', line)
  49. print(changes)
  50. #print(changes , file = new_code)
  51.  
  52.  
  53.  
Success #stdin #stdout 0.02s 9464KB
stdin
Standard input is empty
stdout
the old change is testing.pickup_start_at
the old model example two is simple.orders2
the exmple with colons: pickup_drop_at
the exmple with colons:pickup_drop_at
  model: company2
model: company2 
model :company2
model : company2
model:company2 
profit
the company1 : company2
old model: company2
the old model is called: company1
the.OTP_BONUS_APPLIED: 57
the.Order_IS_Cancelled: Order is Cancelled
track: do.is_taken_y/n my name
so: [incentives.salary decrease]
testing
testing
field: [orders.orders2,orders.delivered,orders.total_payout
orders.pickup_start_at,orders.Order_IS_Cancelled]