Fork me on GitHub

  做音频处理过程中,经常遇到需要对文本进行转换,今天就遇到了一个行末加逗号的问题,找到了几种有效的方式,做个记录吧。

以下是几种方法实现:

python代码实现:

import os

with open('input.txt', 'rb') as lines:
     with open('output.txt', 'wb') as outfile:
        for line in lines:
            line = '"' + line.replace(os.linesep, "") + '",' + os.linesep
            outfile.write(line)

  亲测有效:

经常使用linux脚本的同学,找到了更简洁的方法,让人赞叹不已:

sed:

cat input.txt | sed 's/$/,/'

awk:

cat input.txt | awk '{print ""$0"\,"}'

xargs:

cat input.txt | xargs printf '%s,\n'

     

每日一言:不积跬步,无以至千里,不积小流,无以成江河。

 

 参考文档:https://blog.csdn.net/bdss58/article/details/54584010 

posted on 2018-10-12 14:40  虚生  阅读(1160)  评论(0编辑  收藏  举报