蟒蛇或bash - 添加"在线路和&QUOT年初;,在行结束 [英] python or bash - adding " at beginning of line and ", at end of line

查看:74
本文介绍了蟒蛇或bash - 添加"在线路和&QUOT年初;,在行结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

first line
line nr 2
line three

和我要生成

"first line",
"line nr 2",
"line three",

我不知道这在python或者在bash做,如果它更容易/更快。我知道有不同的code打开文件,不同的阅读只有一个在Python(?)线,但我不知道在这种情况下要使用的选项,更重要的是,如何添加这些字符。任何意见将帮助。

I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different code for opening file and different for reading only one line in python(?) but i'm not sure which option to use in this case and, more importantly, how to add these characters. Any advice would help.

推荐答案

一些简单的方法来做到这一点...

A number of easy ways to do it...

一个简单的Perl oneliner:

A simple perl oneliner:

perl -pi -e 's/^(.*)$/\"$1\",/g' /path/to/your/file

要解释一下,正则表达式 ^(。*)$ 抓住一切(的(。*) )该行的开始之间( ^ )和线(末 $ ),然后使用 $ 1 比赛组变量与引号和逗号重建它。

To explain a bit, the regex ^(.*)$ grabs everything (the (.*)) between the start of the line (^) and the end of the line ($), then uses the $1 match group variable to reconstruct it with the quotes and comma.

这篇关于蟒蛇或bash - 添加"在线路和&QUOT年初;,在行结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆