MBasith MBasith - 2 months ago 5
Python Question

Python handling NoneType when parsing tables

I am trying to compare two tables (table_a and table_b) and subtract the last column of table_a from the last column of table_b. However, table_a includes an extra row and is causing me to get a NoneType Error. Is there a away I can still include the "Plums" row from table_a and just output 'NULL' for the delta cell? Below is my testable code.

Current Code:

from datetime import datetime
import itertools

table_a = (
(datetime(2016, 9, 28, 0, 0), 'Apples', 650, 700, 850),
(datetime(2016, 9, 28, 0, 0), 'Oranges', 900, 950, 1000),
(datetime(2016, 9, 28, 0, 0), 'Grapes', 1050, 1100, 1150),
(datetime(2016, 9, 28, 0, 0), 'Plums', 2000, 3000, 4000)
)

table_b = (
(datetime(2016, 9, 27, 0, 0), 'Apples', 50, 150, 200),
(datetime(2016, 9, 27, 0, 0), 'Oranges', 250, 350, 400),
(datetime(2016, 9, 27, 0, 0), 'Grapes', 450, 550, 600),
)

table_format = '{:<10}|{:<8}|{:<8}|{:<8}|{:<8}|{:<12}'
line_sep = ('-' * 60)

print(line_sep)
print(table_format.format('Date', 'Count_1', 'Count_2', 'Count_3' , 'Count_4', 'Count_4_Delta'))


for a, b in itertools.zip_longest(table_a, table_b):
l = str(a[0])[0:10]
m = a[1]
n = a[2]
o = a[3]
p = a[4]
q = b[4]
print(line_sep)
print(table_format.format(l, m, n, o, p, (p-q)))


Output with Error:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550
Traceback (most recent call last):
File "/media/test.py", line 30, in <module>
q = b[4]
TypeError: 'NoneType' object is not subscriptable


If I add a if statement to remove NoneType it prints the table without an error but excludes the "Plums" row.

for a, b in itertools.zip_longest(table_a, table_b):
if a and b is not None:
l = str(a[0])[0:10]
m = a[1]
n = a[2]
o = a[3]
p = a[4]
q = b[4]
print(line_sep)
print(table_format.format(l, m, n, o, p, (p-q)))


Output with If Statement:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550


I would like to have the below output. Where the "Plums" row still prints but has the string 'NULL" for the delta cell.

Desired Output:

------------------------------------------------------------
Date |Count_1 |Count_2 |Count_3 |Count_4 |Count_4_Delta
------------------------------------------------------------
2016-09-28|Apples |650 |700 |850 |650
------------------------------------------------------------
2016-09-28|Oranges |900 |950 |1000 |600
------------------------------------------------------------
2016-09-28|Grapes |1050 |1100 |1150 |550
------------------------------------------------------------
2016-09-27|Plums |2000 |3000 |4000 |NULL

Answer

itertools.zip_longest accepts an optional fillvalue parameter. If it's provided, it is used instead of None:

>>> list(itertools.zip_longest([1, 2, 3], [4, 5]))
[(1, 4), (2, 5), (3, None)]
>>> list(itertools.zip_longest([1, 2, 3], [4, 5], fillvalue='NULL'))
[(1, 4), (2, 5), (3, 'NULL')]

You can provide empty row (a list of NULL values) as the fillvalue:

class EmptyValue:
    def __sub__(self, other):
        return 'NULL'
    def __rsub__(self, other):
        return 'NULL'

empty_row = [None, 'NULL', EmptyValue(), EmptyValue(), EmptyValue()]
for a, b in itertools.zip_longest(table_a, table_b, fillvalue=empty_row):
    ...
Comments