Convert a space delimited file to comma separated values file in python

前端 未结 8 1047
陌清茗
陌清茗 2021-01-04 06:11

I am very new to Python. I know that this has already been asked, and I apologise, but the difference in this new situation is that spaces between strings are not equal. I

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 06:44

    >>> a = 'cah  1  C       6.00    0.000000000    1.342650315    0.000000000'
    =>  a = 'cah  1  C       6.00    0.000000000    1.342650315    0.000000000'
    
    >>> a.split()
    =>  ['cah', '1', 'C', '6.00', '0.000000000', '1.342650315', '0.000000000']
    
    >>> ','.join(a.split())
    =>  'cah,1,C,6.00,0.000000000,1.342650315,0.000000000'
    
    >>> ['"' + x + '"' for x in a.split()]
    =>  ['"cah"', '"1"', '"C"', '"6.00"', '"0.000000000"', '"1.342650315"', '"0.000000000"']
    
    >>> ','.join(['"' + x + '"' for x in a.split()]
    =>  '"cah","1","C","6.00","0.000000000","1.342650315","0.000000000"'
    

提交回复
热议问题