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

Time:07-14

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

CodePudding user response:

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)
  • Related