Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set python generated table for each value in separate cells *.CSV

I am trying to generate .CSV file (or.txt). The script is simple. However, the generated *.CSV file is putting all values in one cell in each row. I want each value to be in separate cell.
Kindly advise.

from tabulate import tabulate
nestedlist = [["Point 1",0,5,0],
             ["Point 2",0,0,0],
             ["Point 3",5,0,0],
             ["Point 4",5,5,0],]

with open('GCP.csv', 'w') as f:
    f.write(tabulate(nestedlist, headers=['n','x','y','z'],tablefmt="plain"))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I suggest using pandas:

import pandas as pd
nestedlist = [["Point 1",0,5,0],
             ["Point 2",0,0,0],
             ["Point 3",5,0,0],
             ["Point 4",5,5,0],]

df = pd.DataFrame(nestedlist, columns=['n','x','y','z'])
df.to_csv('GCP.csv', index=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading