Python: Determine Missing Line
-
23-06-2021 - |
Question
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.
Solution
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.
OTHER TIPS
if line.count('Apple')==1:
Apple.append(str(number))
else:
Apple.append('-')
from collections import defaultdict
from itertools import takewhile
d = defaultdict(list)
valid_fruits = set(['Apple', 'Pear', 'Orange'])
with open('data.txt') as f:
while True:
fruits = dict(takewhile(lambda x: x[0] != ';',
(line.rstrip().split(' = ') for line in f)))
if not fruits:
break
for fruit in valid_fruits:
if fruit in fruits:
d[fruit].append(int(fruits[fruit]))
else:
d[fruit].append('-')
print d
Output:
defaultdict(<type 'list'>, {'Orange': [2, 3, 1], 'Pear': [1, 1, 1], 'Apple': [1, '-', 1]})