I have few thousands of text files where each file is of following form:
Some Key: value1
Some Other Key: value2
Another Key: value3
... < 300+ such entries > ...
I want to read each of these files as dictionary and populate as a row in pandas dataframe. Though at this moment I am not sure if all the files have exact same keys or not but I'm hoping that there will not be too much variations as these files are logs from some tool.
What is the easiest way to read each file as dictionary so that it's correct by constructions? As of now, my simple code is like following:
with open(log_data_file, mode="r") as txt_file:
for line in txt_file:
keyval = line.strip().split(sep=":", maxsplit=3)
if len(keyval) != 2:
# some debug print
continue
data[keyval[0]] = keyval[1]
Possibly, I can add some logic to handle a line if that satisfies a regular expression. But beyond that, is there package in python where I can specify the grammar for the file and handle [iterate over list of (key, value)] the file only if the grammar is satisfied and file read is successful?
data[keyval[0]] = keyval[1].strip()