I am having a hard time implementing/searching how to format the text/string while writing to a CSV File. Below is the dictionary attendance
I am writing to file.csv
File.
attendance = [{'NAME': 'FIRST_NAME_1 LAST_NAME_1', 'ROLL NUMBER': '1XXXXXXXXX', '29/04/2018': 'PRESENT', '30/04/2018': 'ABSENT', '01/05/2018': 'PRESENT'},
{'NAME': 'FIRST_NAME_2 LAST_NAME_2', 'ROLL NUMBER': '2XXXXXXXXX', '29/04/2018': 'PRESENT', '30/04/2018': 'PRESENT', '01/05/2018': 'PRESENT'},
{'NAME': 'FIRST_NAME_3 LAST_NAME_3', 'ROLL NUMBER': '3XXXXXXXXX', '29/04/2018': 'PRESENT', '30/04/2018': 'ABSENT', '01/05/2018': 'ABSENT'},
{'NAME': 'FIRST_NAME_4 LAST_NAME_4', 'ROLL NUMBER': '4XXXXXXXXX', '29/04/2018': 'ABSENT', '30/04/2018': 'ABSENT', '01/05/2018': 'ABSENT'},
................................
................................
................................
................................
{'NAME': 'FIRST_NAME_n LAST_NAME_n', 'ROLL NUMBER': 'nXXXXXXXXX', '29/04/2018': 'PRESENT', '30/04/2018': 'PRESENT', '01/05/2018': 'PRESENT'}]
Below is the code for writing to file.csv
.
import csv
CSV_Fields = ['NAME', 'ROLL NUMBER', '29/04/2018', '30/04/2018', '01/05/2018']
with open(filename, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=CSV_Fields)
writer.writeheader()
writer.writerows(attendance)
I tried following this answer but could not implement it and I tried searching the web came over these formatting options in Python. Please anyone can help me out. Thank You!
Output (from the above code)
Or If I can do cell alignment too as shown in the below format.