parsing records with key value pairs in python -
i have file millions of records this
2017-07-24 18:34:23|cn:ssl|responsetime:23|bytesize:1456|clientip:127.0.0.9|protocol:ssl-v1.2
each record contains around 30 key-value pairs "|" delimeter. key-value pair position not constant. trying parse these records using python dictionary or list concepts. note: 1st column not in key-value format
your file |
-separated csv
file holding first timestamp, 2 fields separated :
.
so use csv
module read cells, pass result of str.split
dict
in gencomp build dictionary elements first one.
then update dict timestamp:
import csv list_of_dicts = [] open("input.txt") f: cr = csv.reader(f,delimiter="|") row in cr: d = dict(v.split(":") v in row[1:]) d["date"] = row[0] list_of_dicts.append(d)
list_of_dicts
contains dictionaries like
{'date': '2017-07-24 18:34:23', 'protocol': 'ssl-v1.2', 'responsetime': '23', 'cn': 'ssl', 'clientip': '127.0.0.9', 'bytesize': '1456'}
Comments
Post a Comment