Python Programming tutorial and exercise
Python Programming to create a csv file json file example
Create a CSV file using a JSON file in python programming
# python programming for creating csv from json file
# python programming tutorial
import json
import csv
# reading json file using json library in python
# converting json into python dictionary
# extracting keys from python dictionary
with open('student_info.json') as file:
data = json.load(file)
li = list(data[0].keys())
# creating csv file in python programming
# creating csv file in file append mode
# writing python list or dictionary into csv file using csv library
with open('students.csv', 'a+', newline="") as cfile:
writer = csv.writer(cfile)
writer.writerow(li)
for item in data:
row = [item['id'], item['name']]
writer.writerow(row)
# example of json file
[
{
"id": 1,
"name": "std1"
},
{
"id": 2,
"name": "std2"
}
]
# output file will be csv file
id,name
1,std1
2,std2
Comments
Post a Comment