0

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)

enter image description here

Desired Output(1) enter image description here

Or If I can do cell alignment too as shown in the below format.

Desired Output(2) enter image description here

Aadit
  • 199
  • 1
  • 6
  • 17
  • What do you mean by cell-width? As mentioned in one of the comments on the question you linked to, the csv format doesn't have a concept of "cell width" like you would think of in an Excel file - it is only the values of each entry, separated by (typically) commas and new lines. – Mostly Harmless Apr 29 '18 at 21:09
  • Yes sorry, Question updated. So is there any way to do this by *string-formatting* in Python? @MostlyHarmless – Aadit Apr 29 '18 at 21:12
  • 1
    If you know the format of the file output you want, sure, but you aren't going to be able to be able to output it to a csv file. [This question](https://stackoverflow.com/q/13437727/2884547) has a few suggestions for libraries that might be useful to writing to .xlsx files, I haven't used them myself but they might have functions to set formatting like you want. – Mostly Harmless Apr 29 '18 at 21:23
  • Yes, I read this can be done by using *xlsx module*, you can manage *Column-witdth* too in that. So there is no way I can implement what I want using *csv module*? @MostlyHarmless – Aadit Apr 29 '18 at 21:31
  • 1
    Correct, because the CSV file format (and thus the csv module) has no concept of nor way to store things that are not actual entries. If you want to arrange how Excel or similar programs layout the contents, you'll have to use another file format, such as .xlsx – Mostly Harmless Apr 29 '18 at 21:35
  • Okay, Thank you so much mate @MostlyHarmless – Aadit Apr 29 '18 at 21:37

0 Answers0