0

I am writing a python program to write data to CSV in Python. This is my code below

from pandas.io.json import json_normalize
import json
import csv

data =[
    {
        "Name": "jonathan",
        "Age": 23,
        "Occupation": "Lawyer",
        "Address":[
            {"postal_code":2323,
             "place": "TP",
             "Location":"Central Singapore"

            }
        ]      

    },
    {
        "Name": "jacky",
        "Age": 21,
        "Occupation": "IT analyst",
        "Address":[
            {"postal_code":31234,
             "place": "CCK",
             "Location":"NW Singapore"

            }
        ]
    }
]

nested_json = data
new_dict= dict()

# to flatten the json
def flatten_json(nested_json):
    out = {}
    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(nested_json)
    return out

# function to get the data out and flatten and write to csv
def write_to_csv(nested_json):
    for i in nested_json:
        a = flatten_json(i)
        print(a)
        with open('dict.csv', 'w', newline='') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow(a.values())

if __name__ == '__main__':
    write_to_csv(nested_json);

The issue is my open csv is writing a blank line after each row of data.

I followed this stackoverflow issue to resolve (CSV file written with Python has blank lines between each row)

but it seems that it is not working. As such there is an alternative way which is to use pandas to remove the blank lines after processing but it seems kinda silly. AS such, May I ask what am I doing wrong here? I ensured that the newline=''.

Thank you very much

adr
  • 55
  • 2
  • 13
  • Could you post an example output of print(a), what the data is at that point? – Sven Hakvoort Dec 07 '18 at 13:37
  • 1
    in `flatten_json` you return `out`, which is initialized as an empty dict but never updated. So you are always returning an empty dict –  Dec 07 '18 at 13:38
  • Where does the `recursive` function come from? – Hai Vu Dec 07 '18 at 13:40
  • you never call a function/execute a code that writes to a file. and `recursive` function is not defined. show your actual code. – buran Dec 07 '18 at 13:44
  • Dear all, my sincere apologies. I changed the method. its write_to_csv. I wrote wrongly earlier and my sincere apologies for that – adr Dec 07 '18 at 13:58

1 Answers1

0

I managed to figure out what was wrong.

Please refer to the revised code here. I will close the issue.

from pandas.io.json import json_normalize
import json
import csv

data =[
    {
        "Name": "jonathan",
        "Age": 23,
        "Occupation": "Lawyer",
        "Address":[
            {"postal_code":2323,
             "place": "TP",
             "Location":"Central Singapore"

            }
        ]      

    },
    {
        "Name": "Adrian",
        "Age": 21,
        "Occupation": "IT analyst",
        "Address":[
            {"postal_code":31234,
             "place": "CCK",
             "Location":"NW Singapore"

            }
        ]
    }
]

nested_json = data
new_dict= dict()

# to flatten the json
def flatten_json(nested_json):
    out = {}
    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(nested_json)
    return out

# recursiveness to get the data out and flatten
def write_to_csv(nested_json):
    with open('dict.csv', 'a', newline='') as csv_file:
        writer = csv.writer(csv_file)
        for i in nested_json:
            a = flatten_json(i)
            print(a)
            writer.writerow(a.values())

if __name__ == '__main__':
    write_to_csv(nested_json);
NickD
  • 5,937
  • 1
  • 21
  • 38
adr
  • 55
  • 2
  • 13
  • `writer = csv.writer(csv_file)` is part of the initialization: move it out of the for-loop. – NickD Dec 07 '18 at 14:04