Python handling NoneType when parsing tables
<p>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.</p>

<p>Current Code:</p>

<pre><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 = '{:&lt;10}|{:&lt;8}|{:&lt;8}|{:&lt;8}|{:&lt;8}|{:&lt;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)))
</code></pre>

<p>Output with Error:</p>

<pre><code>------------------------------------------------------------
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 &lt;module&gt;
    q = b[4]
TypeError: 'NoneType' object is not subscriptable
</code></pre>

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

<pre><code>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)))
</code></pre>

<p>Output with If Statement:</p>

<pre><code>------------------------------------------------------------
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              
</code></pre>

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

<p>Desired Output:</p>

<pre><code>------------------------------------------------------------
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        
</code></pre>

<p><a href="https://docs.python.org/3/library/itertools.html#itertools.zip_longest" rel="nofollow">zip_longest</a> returns a singular <code>None</code> type when it runs out of values. You want a list of <code>None</code>s or you get a <code>TypeError</code> when you try and use the subscript <code>[]</code> operator. </p>

<p>Use the optional fillvalue to get a list of <code>None</code>s and then test for <code>None</code> when you format for output so you don't get another <code>TypeError</code> when you try and do <code>p-q</code> when <code>q</code> is <code>None</code>:</p>

<pre><code>for a, b in itertools.zip_longest(table_a, table_b,fillvalue=[None]*5):
    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) if q is not None else 'NULL'))
</code></pre>

python python-3.x parsing itertools nonetype