How to write row only if value is not null?

Viewed 276

I am converting my json data that I made an api request to to CSV, however some keys in the object have null values. How can I only write rows that have non null values?

count = 0

for stats in nba_team_stats:
    if count == 0:
        header = stats.keys()
        csv_writer.writerow(header)
        count += 1
    csv_writer.writerow(stats.values())
data_file.close()
2 Answers

Assuming you are using the python csv.DictWriter, the following example works fine. You don't need a loop to do this.

from csv import DictWriter

#Ignore this. This was a test without a file
import io
f = io.StringIO()
f.nam = "t.csv"

nba_team_stats = [{"a": None, "b": 10}, {"b": None, "a": 10}]
csv_writer = DictWriter(f, fieldnames=list(nba_team_stats[0].keys()))
csv_writer.writeheader()
csv_writer.writerows(nba_team_stats)

print(f.getvalue())

Output

a,b
,10
10,

Raw python:

import csv
from itertools import chain

it = iter(nba_team_stats)
first_row = next(it, None)
if first_row:
    rows = chain((first_row,), it)

    with open("results.csv", "w") as f:
        writer = csv.writer(f)
        # writing keys
        writer.writerow(first_row)
        # writing content, but:
        # ASSUMING EVERY DICT WAS CREATED WITH THE SAME INSERTION ORDER AND PYTHON
        # VERSION IS 3.6 OR HIGHER
        writer.writerows(
            values for values in (row.values() for row in rows) if any(values)
        )

The following convtools based solution is an alternative one:

from convtools import conversion as c
from convtools.contrib.tables import Table

table = Table.from_rows(nba_team_stats, header=True)
columns = table.columns
table.filter(
    c.or_(*(c.col(column) for column in columns), default=False)
).into_csv("results.csv")
Related