Skip to content
Advertisement

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"))

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement