Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. [BEGIN_PATTERN]
  2. line1=abd;
  3. line2=ZXY;
  4. ...
  5. line43=454;
  6. ...
  7. ...
  8. [END_PATTERN]
  9. [BEGIN_PATTERN]
  10. line1=abc;
  11. line2=ZXC;
  12. ...
  13. line72=847;
  14. ...
  15. [END_PATTERN]
  16. [BEGIN_PATTERN]
  17. line1=abe;
  18. line2=ZXV;
  19. ...
  20. line33=135;
  21. ...
  22. [END_PATTERN]
  23. [BEGIN_PATTERN]
  24. line1=abt;
  25. line2=ZXF;
  26. ...
  27. line54=734;
  28. ...
  29. [END_PATTERN]
  30.  
  31. abd,ZXY,aaa,454,ggg,ggs
  32. abc,ZXC,mgf,847,jde,g3e
  33. abe,ZXV,ytd,135,dfs,jhf
  34. abt,ZXF,ytf,734,ytd,hge
  35.  
  36. import re
  37.  
  38. START_PATTERN = '<BEGIN'
  39. END_PATTERN = '<BEND'
  40.  
  41. with open('DB_example.txt') as file:
  42. match = False
  43. newfile = None
  44.  
  45. for line in file:
  46. if re.match(START_PATTERN, line):
  47. match = True
  48. newfile = open('my_new_file.txt', 'w')
  49. continue
  50. elif re.match(END_PATTERN, line):
  51. match = False
  52. newfile.close()
  53. continue
  54. elif match:
  55. #remove TAB and BreakLine
  56. valor=line.rstrip().replace('t','')
  57. #split Key and value
  58. (key, val) = valor.split('=')
  59. if re.match('line1',key):
  60. match = True
  61. #before write into file remove ";"
  62. newfile.write(val.replace(';',''))
  63. continue
  64. elif re.match('line2',key):
  65. match:False
  66. newfile.write(','+val.replace(';', ''))
  67. continue
  68. elif re.match('lineXX',key):
  69. match:False
  70. newfile.write(','+val.replace(';', ''))
  71. continue
  72. elif re.match('lineYY',key):
  73. match:False
  74. newfile.write(','+val.replace(';', ''))
  75. continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement