I have a file which is having pipe delimiter and wanted to convert delimiter from | to ^A Source file has data in key:Value pair. For eg. in the below given file SSLPROTOCOL is the Key and TLSv1.2 is value. I was looking for only the value with delimiter in the target file. Please sugesst
JavaScript
x
2
1
2017-04-05 16:52:45.564|CN:-|SSLPROTOCOL:TLSv1.2|SSLCIPHER:ECDHE-RSA-AES256-GCM-SHA384|HTTPMETHOD:PATCH|URI:/portal/cache/num_active_capione_nodes|RESPONSETIME:9|STATUS:200|CLIENTIPADDRESS:10.202.2.123|CLIENT:portal-admin@capitalone.com|RESPONSEBODYSIZE:12
2
Output: I want values delimited with ^A
, only with values e.g.:
JavaScript
1
2
1
TLSv1.2 ^A ECDHE-RSA-AES256-GCM-SHA384 ^A PATCH ^A /portal/cache/num_active_capione_nodes ^A 9 ^A 200 ^A 10.202.30.123 ^A portal-admin@capitalone.com ^A 12
2
Advertisement
Answer
This should do it:
JavaScript
1
6
1
with open("foo.dat", "r+") as f:
2
data = f.read()
3
data = ata.replace('|', ' ^A ')
4
f.seek(0)
5
f.write(data)
6