Skip to content
Advertisement

Extracting values from .dat file in python

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

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

Output: I want values delimited with ^A, only with values e.g.:

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

Advertisement

Answer

This should do it:

with open("foo.dat", "r+") as f:
    data = f.read()
    data = ata.replace('|', ' ^A ')
    f.seek(0)
    f.write(data)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement