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

Monday, August 29, 2022

[FIXED] How to save a list as a CSV file with new lines?

 August 29, 2022     csv, list, pandas, python     No comments   

Issue

I would like to save a Python list in a CSV file, for example I have a list like this:

['hello','how','are','you']

I would like to save it as:

colummn,
hello,
how,
are,
you,

I tried the following:

myfile = open('/Users/user/Projects/list.csv', 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL,'\n')
wr.writerow(pos_score)

Solution

use pandas to_csv (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_csv.html)

>>> import pandas as pd
>>> df = pd.DataFrame(some_list, columns=["colummn"])
>>> df.to_csv('list.csv', index=False)


Answered By - grechut
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