-1

I am going to read a file in python line by line and convert the line to a dictionary. The structure of the line is a little complex and I have no idea how can I convert that in a dictionary. Here is an example of first line of my file:

OrderedDict([('key', 'my_key'), ('rank', '2'), ('origin', 'IR'), ('num', '10'), ('num_2', '220'), ('same_array', ['a','b','d']), ('dif_array', ['g','h']), ('first_dic', OrderedDict([('name','ali'),('last','ali')])), ('num_', 0), ('response', OrderedDict([('ok_1', 0), ('red', 0), ('bad', 0)])), ('response_code', OrderedDict([('ok', 0), ('red', 0), ('bad', 0)]))])

I want to read first line and save that in a dictionary in a way that can use commands such as print my_dictionary['key'] which I want to got my_key .

This is a very complex file which as you can see there are some inner array and dictionary in each line. I tried to use some command such as new_line = dict(item.split("=") for item in line.split(",")) or new_line = ast.literal_eval(line), but I got error because for example for first command as there are so many , inside the inner array, I do not got the good separation of the main dictionary items or for the second command I got this error:

ValueError: malformed string
  • Your example contains no equals signs. Can you post what the actual file looks like? Why does your file contain `OrderedDict`? – Blender Jan 01 '18 at 04:03
  • It looks like your file contains Python code. Either eval it line by line or load it as a module. – Mad Physicist Jan 01 '18 at 04:03
  • Might be relevant: https://stackoverflow.com/q/2349991/2988730 – Mad Physicist Jan 01 '18 at 04:05
  • I have used script to generate my ordered dictionary and then I am saving the dictionary in the file. Now I want to pars the file. The content of each line is huge and I can not post the whole content here. – Shahrooz Pooryousef Jan 01 '18 at 17:41

1 Answers1

0

it seems like you already try to eval the line but it is malformed in this part: 'innerarray: ['one', 'two'] you can even tell from the highlighting of the code that there is an apostrophe missing after 'innerarray

if the file was created by a script it probably means there are similar errors in other lines, but if the file was made by hand it might be the only error or it could be any number of combinations of errors that caused this to happen

i would suggest to either share how this file was created (because something went wrong with its creation) or just fix the file manually

AntiMatterDynamite
  • 1,495
  • 7
  • 17
  • I have created the file by an script. I have just saved my dictionary in the file and right now I want to pars that. The innerarray error just has been happen during copy pasting the file content here. – Shahrooz Pooryousef Jan 01 '18 at 17:24
  • i think you still have some other errors in your line, there are definitely missing parenthesis at the end of it... i suggest you either paste the actual full line or rephrase the question to something like: "how to save and load a complex python data structure to file" – AntiMatterDynamite Jan 02 '18 at 12:19
  • I did it. Thanks. – Shahrooz Pooryousef Jan 03 '18 at 15:31