需要对csv文件的每个值应用双引号 [英] Need to apply double quotes around every value of csv file

查看:359
本文介绍了需要对csv文件的每个值应用双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,我需要每一列包装在引号中。如何使用regex更改它?

I have a csv file that I need EVERY column wrapped in quotes. How do I change that using regex?

我使用我的文本编辑器来执行find / replace w /正则表达式(textmate)

I am using my text editor to do the find/replace w/ regular expression (textmate)

example csv:

example csv:

bear,brown,mean,large
ant,black,strong,tiny
cat,yellow,moody,small


推荐答案

p>这里是正则表达式的重要部分。希望当我转换为textmate格式时,我得到它:

Here are the important portions of the regex. Hopefully I got it right when I converted to textmate format:

搜索 - ([^,] *)(,| $)

替换 - $ 1$ 2

搜索说明:查找不是逗号的每个字符,直到到达逗号或行尾。捕获要在一个变量中引用的字符串的匹配,并在另一个变量中捕获逗号/行尾匹配。

Search explanation: Find every character that is not a comma, up until we reach a comma, or the end of the line. Capture the match for string to be quoted in one variable, and capture the comma/end-of-line match in another variable.

替换解释:引用的原始字符串,以及其后的逗号或行尾。

Replace explanation: The original string, quoted, and the comma or end-of-line that follows it.

这篇关于需要对csv文件的每个值应用双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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