The flatten function (
|
def flatten(d, parent_key='', sep='__'): |
) will return a dictionary with the correctly flattened values if a nested value exists. However, if another record in the same input source contains the same key and is blank, the recursive call to
flatten will not return anything, and so that key will not be present in the return value.
In the example above, the dictionaries returned from flatten will have different numbers of keys, which causes a problem when writing the output to a CSV (i.e. the number of columns will differ).