How to extract a list of dict from a text file using python


#1

I’ve a log file which contains logs along with some results which are a list of dictionaries.
I want to extract the where this list of dict are there into other file.
Please help to achieve this ASAP.

Thanks in advance !


#2

Hi @jayanthireddy,

Can you show us your code? Also it will be good if you can let us know where are you stuck so that we can help you better.

Thanks


#3

My file is a log file. The data which I want to extract looks like

“2018-11-27 22:31:04 INFO life_cycle_manager.py:310 (TB) Entity {‘count’: 1, ‘entity_class’: ‘Hardware Entities (AHV)’, ‘description’: ‘Firmware payload version applied on the node’, ‘zprotobuf’: ‘eNrjUjEwSLVIMzU31U2xNLXUNUkxT9O1MDNI1DUzTE6yMDExMEgyTxTSzMtPSbVKsUgzszCxMNU1tjQEKjUzSwIqNQSqNzVNMzU0NDNPNUtREvdILEopTyxKVXDNK8ksyUwtVtBw9AjT1JIyNHZXcMssygVLBiRW5uQnpihopOaYaRrxGekZ6xqamhiaGluaGJpYcaek5uTEp6fmxRsaO+nANRVANZWlFhVn5ucpJBYU5GSmpigAmSUZqQogR3owJvoc3bZqczUrAwDgvUFx’, ‘last_detected_time_usecs’: 1543386477020492, ‘hw_family’: ‘dell_gen_13’, ‘version’: ‘2.3-1541539414’, ‘entity_model’: ‘13G Firmware Payload (el6)’, ‘location_id’: ‘node:d8f68485-3919-466b-861f-855f51167e6d’, ‘uuid’: ‘00e8f575-d959-4d7f-860a-61cb84400b7a’} has available versions [{‘entity_class’: ‘Hardware Entities (AHV)’, ‘status’: ‘recommended’, ‘uuid’: ‘de74178a-cbc7-4c69-ae2f-9e7042bf8e98’, ‘zprotobuf’: ‘eNolzD1LAzEYB/DFScHBRXAKoUMr5khiLi/dCgpdCg7qIiLPJU/KQS4nyeEL6ne3p+vv/3K8CGiUMBaY77xhymvHAGVkDg1XsosWnT1bcI42tqZlwbWOqWAis5oD08J3VinOOwMXp7JRTLRKam05v6YnBf04DJgDhsvvpy+KeeqnzxefoFa6JvQGUyIPrwEmJDvIsMdCrwh9w1L7Mc8N0VjGGyHk7PgBfpo1Qqr4J/+HwxgwzcHdPdnsD0jGTDbbR7LEpFf055kfrc+3UMI7FCS386jHSpaHyuoXHmhKNg==’, ‘version’: ‘2.4-1542668003’, ‘dependencies’: ‘[{“entity_class”: “Dell Update Manager”, “version”: “1.8-0.112”, “exact”: “false”, “entity_model”: “PT Agent on AHV (el6)”}]’, ‘entity_uuid’: ‘00e8f575-d959-4d7f-860a-61cb84400b7a’, ‘order’: 4}]”

  1. I want to extract the lines which looks like this. (I’ve done it)
  2. In this matched line I want to extract the data which matched for “entity_class” and “entity_model” and then write into an excel file.

I need help for the 2nd step.


#4

I have achieved the second step almost all.

I am trying to convert the str into dict type using json.loads() method, but it’s failing with the error:

File “/usr/local/lib/python2.7/json/init.py”, line 339, in loads
return _default_decoder.decode(s)
File “/usr/local/lib/python2.7/json/decoder.py”, line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/local/lib/python2.7/json/decoder.py”, line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

can someone help me.