如何在 vim 中处理 csv 文件
我希望 csv 文件在 vim 中打开,就像在 microsoft office 中打开一样。数据应采用列格式,不应出现逗号,并且应易于遍历。是否可以在 vim 中借助任何插件来实现?
I want csv file to be opened in vim in the same way it opens in microsoft office . Data should be in column format and commas should not be seen and its should be traversed easily. Is it possible in vim with help of any plug-ins?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我可能会稍后回答这个问题,但为了完整起见,我无论如何都会回答。我已经制作了 csv 插件,它应该可以做你想做的事情想。
其中,它允许:
:SearchInColumn
命令在列中搜索文本:HiColumn 突出显示光标所在的列
命令:ArrangeColumn
命令直观地排列所有列 使用:DeleteColumn
命令删除列:Header
显示垂直或水平标题行> 或:VHeader
命令:Sort
命令对列进行排序:Column
命令复制列进行注册 使用:SumCol
命令计算列中所有值的总和(您还可以定义自己的自定义聚合函数)I am probably a little bit later answering that question, but for completeness I'll answer anyway. I have made the csv plugin that should make it possible to do what you want.
Among others, it allows:
:SearchInColumn
command:HiColumn
command:ArrangeColumn
command:DeleteColumn
command:Header
or:VHeader
command:Sort
command:Column
command:MoveCol
command:SumCol
command (you can also define your own custom aggregate functions)还有 rainbow_csv vim 插件。它将用不同的“彩虹”颜色突出显示 csv/tsv 文件列,并允许您使用 Python 或 JavaScript 表达式编写类似 SQL 的 SELECT 和 UPDATE 查询。
There's also exist rainbow_csv vim plugin. It will highlight csv/tsv file columns in different "rainbow" colors and will allow you to write SQL-like SELECT and UPDATE queries using Python or JavaScript expressions.
我尝试过 Christian 的 csv 插件,它对于快速查看 csv 文件很有用,特别是当您需要查看许多不同的文件时。
然而,当我要多次查看同一个 csv 文件时,我会将文件导入到 sqlite3 中,这使得进一步分析更快更容易执行。
例如,如果我的文件如下所示:
我创建一个新的 sqlite 数据库(从命令行):
然后在数据库中创建一个表以将文件导入到:
现在新表“mytable”包含文件中的数据,但第一行存储的是您通常不需要的标题,因此您需要删除它(在字段值周围使用单引号;如果您使用双引号,您将删除所有行):
现在您可以轻松查看数据,按复杂公式过滤,按多个字段排序等。
(抱歉,这变成了 sqlite 教程,但似乎每次我不导入到 sqlite 中时,我最终都会花费使用 vim/less/grep/sort/cut 的时间比我刚导入的时间要多得多)。
I've tried Christian's csv plugin, and it is useful for quick looks at csv files, especially when you need to look at many different files.
However, when I'm going to be looking at the same csv file more than a few times, I import the file into sqlite3, which makes further analysis much faster and easier to perform.
For instance, if my file looks like this:
I create a new sqlite db (from the command line):
Then create a table in the db to import the file into:
Now the new table 'mytable' contains the data from the file, but the first row is storing the header, which you don't typically want, so you need to delete it (use single quotes around the field value; if you use double quotes you'll delete all rows):
Now you can easily look at the data, filter by complex formulas, sort by multiple fields, etc.
(Sorry this turned into a sqlite tutorial but it seems like every time that I don't import into sqlite, I end up spending much more time using vim/less/grep/sort/cut than I would have had I just imported in the first place).
您可能想看看 sc 作为替代方案。看看这个 linux 期刊页面
You probably want to look at sc as an alternative.. Have a look at this linux journal page
以下是在 vim 中处理 CSV 文件的一些提示:
http://vim.wikia.com/wiki/Working_with_CSV_files
我不确定是否有办法在不使用逗号的情况下在列中显示它,尽管该链接中的提示允许 vim 非常轻松地遍历和操作 CSV。
Here's some tips for working with CSV files in vim:
http://vim.wikia.com/wiki/Working_with_CSV_files
I'm not sure if there's a way to display it in columns, without commas, though the tips in that link allow vim to traverse and manipulate CSV very easily.
您可以使用 column< 在 CLI 中直接执行此操作,无需使用插件/a>:
请注意,某些命令选项可能会因您的 unix/linux 风格而异,因此请检查与您的风格相关的手册页。
You can do this right in the CLI without a plugin using column:
Note that some of the command options may differ on your flavor of unix/linux, so check the man page relevant to your flavor.
我使用@chrisbra的插件,
并在页面加载时添加了一个快速命令;
对于大型记录可能存在风险。
I use @chrisbra's plugin,
and I add a quick command on page load;
could be risky on large records.