I have a file that contains following data in it. I am trying to parse the data.
08/23/21 04:00:05 AM
/* ----------------- data1----------------- */
make: honda model: civic
year: 2019
trim: "lx"
owner: phillip
/* ----------------- data2----------------- */
make: toyota model: highlander
year: 2021
trim: "Platinum"
I want to see the data like this:
Make, Model, Year, trim, Owner
Honda, civic, 2019, lx, phillip
toyota, highlander, 2021, platinum, Rex
here is my code: I was tryin to create dictionary and then load to panda dataframe. I think i am not on right direction.
def fix_line(record):
#split every field and value into a seperate line
results = []
mini_collection = []
if not record.startswith("/*"):
#for data in record.rstrip('\n').strip().split(' '):
for data in record.rstrip('\n').split(' '):
if ':' not in data:
mini_collection.append(data)
else:
results.append(data)
return results
def create_dictionary(data):
record = {}
for line in fix_line(data):
line = line.strip()
name, value = line.split(':', 1)
record[name.strip()] = value.strip()
return record