PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

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

 August 28, 2022     csv, python     No comments   

Issue

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

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)


Answered By - RJ Adriaansen
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing