Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [BEGIN_PATTERN]
- line1=abd;
- line2=ZXY;
- ...
- line43=454;
- ...
- ...
- [END_PATTERN]
- [BEGIN_PATTERN]
- line1=abc;
- line2=ZXC;
- ...
- line72=847;
- ...
- [END_PATTERN]
- [BEGIN_PATTERN]
- line1=abe;
- line2=ZXV;
- ...
- line33=135;
- ...
- [END_PATTERN]
- [BEGIN_PATTERN]
- line1=abt;
- line2=ZXF;
- ...
- line54=734;
- ...
- [END_PATTERN]
- abd,ZXY,aaa,454,ggg,ggs
- abc,ZXC,mgf,847,jde,g3e
- abe,ZXV,ytd,135,dfs,jhf
- abt,ZXF,ytf,734,ytd,hge
- import re
- START_PATTERN = '<BEGIN'
- END_PATTERN = '<BEND'
- with open('DB_example.txt') as file:
- match = False
- newfile = None
- for line in file:
- if re.match(START_PATTERN, line):
- match = True
- newfile = open('my_new_file.txt', 'w')
- continue
- elif re.match(END_PATTERN, line):
- match = False
- newfile.close()
- continue
- elif match:
- #remove TAB and BreakLine
- valor=line.rstrip().replace('t','')
- #split Key and value
- (key, val) = valor.split('=')
- if re.match('line1',key):
- match = True
- #before write into file remove ";"
- newfile.write(val.replace(';',''))
- continue
- elif re.match('line2',key):
- match:False
- newfile.write(','+val.replace(';', ''))
- continue
- elif re.match('lineXX',key):
- match:False
- newfile.write(','+val.replace(';', ''))
- continue
- elif re.match('lineYY',key):
- match:False
- newfile.write(','+val.replace(';', ''))
- continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement