Take for example a short file consisting of these lines:
Apple = 1
Pear = 1
Orange = 2
;
Orange = 3
Pear = 1
;
Apple = 1
Pear = 1
Orange = 1
What I did was to readline the text file, and put Apple, Orange and Pear into their respective lists such as:
Orange = [ 2,3,1 ]
Pear = [ 1,1,1 ]
I used line.count('Apple')==1 to identify that the current line in the text file is a fruit and then append the value after = to the list.
As you can see, the second section is missing Apple. I want the list to be:
Apple = [ 1,'-',1]
Whenever the line does not show a fruit, the list should append - to indicate it.
My question is, how then can I do it ie. to determine that a section is missing a fruit name and then to append - to it.
You could keep an array with all the fruit names found in a section. When you encounter a semicolon you check for each fruit if its name exists in the array and append a ‘-‘ if it does not, then you clear the array and continue with the next section.