【问题标题】:How do I rearrange words separated with comma (txt file) to list of words in pandas?如何将用逗号分隔的单词(txt 文件)重新排列为 pandas 中的单词列表?
【发布时间】:2021-06-08 13:55:14
【问题描述】:

我有一个txt文件写成

a,b,c,d,e ...

如何在 python 中阅读并重新排列如下格式?

a
b
c
d
e

非常感谢..

【问题讨论】:

    标签: python pandas txt


    【解决方案1】:

    如果你只是想转置你可以使用:

    pd.read_csv('<Name of your csv file here>', header= None).T.to_csv('test.txt', index =False, header =None)
    

    【讨论】:

      【解决方案2】:
      >>> pd.Series(open("dummy.txt").read().rstrip().split(','))
      0    a
      1    b
      2    c
      3    d
      4    e
      dtype: object
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2015-08-18
      • 2021-01-17
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多