Epython Lab
6.11K subscribers
680 photos
31 videos
104 files
1.31K links
Welcome to Epython Lab, where you can get resources to learn, one-on-one trainings on machine learning, business analytics, and Python, and solutions for business problems.

Buy ads: https://telega.io/c/epythonlab
Download Telegram
#Question_by_user
Q: "Can somebody help me in putting list in CSV file in Python"

#Solution

# first create a list with items you would like to save a csv file
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
{'name': 'Wiltmore Denis', 'userid': 2525942, 'is_admin': False},
{'name': 'Greely Plonk', 'userid': 15890235, 'is_admin': False},
{'name': 'Dendris Stulo', 'userid': 572189563, 'is_admin': True}]
# import csv module
import csv

# create afile with open function with a temporary file name output.csv
with open('output.csv', 'w') as output_csv:
# create a list of headers
fields = ['name', 'userid', 'is_admin']
# write the output using dictionary writter function
output_writer = csv.DictWriter(output_csv, fieldnames=fields)
# write the headers
output_writer.writeheader()
# create for loop which iterate each list and write a row
for item in big_list:
output_writer.writerow(item)
👍3