How to save a list as a .csv file with python with new lines?

后端 未结 3 1589
孤独总比滥情好
孤独总比滥情好 2021-02-02 10:24

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 woul

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 10:58

    You can just pass this as the value of a dict with key 'column' to a DataFrame constructor and then call to_csv on the df:

    In [43]:
    
    df = pd.DataFrame({'column':['hello','how','are','you']})
    df
    Out[43]:
      column
    0  hello
    1    how
    2    are
    3    you
    In [44]:
    
    df.to_csv()
    Out[44]:
    ',column\n0,hello\n1,how\n2,are\n3,you\n'
    

提交回复
热议问题