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