如何在 vim 中处理 csv 文件

发布于 2024-10-19 20:18:10 字数 99 浏览 2 评论 0原文

我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦里°也失望 2024-10-26 20:18:10

我可能会稍后回答这个问题,但为了完整起见,我无论如何都会回答。我已经制作了 csv 插件,它应该可以做你想做的事情想。

其中,它允许:

  • 显示光标所在的列以及列数
  • 使用 :SearchInColumn 命令在列中搜索文本
  • 使用 :HiColumn 突出显示光标所在的列 命令
  • 使用 :ArrangeColumn 命令直观地排列所有列 使用
  • :DeleteColumn 命令删除列
  • 使用 :Header 显示垂直或水平标题行> 或 :VHeader 命令
  • 使用 :Sort 命令对列进行排序
  • 使用 :Column 命令复制列进行注册 使用
  • 将一列移动到另一列后面>:MoveCol 命令
  • 使用 :SumCol 命令计算列中所有值的总和(您还可以定义自己的自定义聚合函数)
  • 使用正常模式命令 (W向前、H 向后、K 向上、J 向下)
  • 设置一个很好的语法突出显示,隐藏分隔符(如果您的 Vim 支持的话)

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:

  • Display on which column the cursor is as well as number of columns
  • Search for text within a column using :SearchInColumn command
  • Highlight the column on which the cursor is using :HiColumn command
  • Visually arrange all columns using :ArrangeColumn command
  • Delete a Column using :DeleteColumn command
  • Display a vertical or horizontal header line using :Header or :VHeader command
  • Sort a Column using :Sort command
  • Copy Column to register using :Column command
  • Move a column behind another column using :MoveCol command
  • Calculate the Sum of all values within a column using :SumCol command (you can also define your own custom aggregate functions)
  • Move through the columns using the normal mode commands (W forwards, H backwards, K upwards, J downwards)
  • sets up a nice syntax highlighting, concealing the delimiter, if your Vim supports it
尸血腥色 2024-10-26 20:18:10

还有 rainbow_csv vim 插件。它将用不同的“彩虹”颜色突出显示 csv/tsv 文件列,并允许您使用 Python 或 JavaScript 表达式编写类似 SQL 的 SELECT 和 UPDATE 查询。

demo rbql 截屏视频

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.

demo rbql screencast

很酷不放纵 2024-10-26 20:18:10

我尝试过 Christian 的 csv 插件,它对于快速查看 csv 文件很有用,特别是当您需要查看许多不同的文件时。

然而,当我要多次查看同一个 csv 文件时,我会将文件导入到 sqlite3 中,这使得进一步分析更快更容易执行。

例如,如果我的文件如下所示:

file.csv:
field1name, field2name, field3name
field1data, field2data, field3data
field1data, field2data, field3data

我创建一个新的 sqlite 数据库(从命令行):

commandprompt> sqlite3 mynew.db

然后在数据库中创建一个表以将文件导入到:

sqlite> create table mytable (field1name, field2name, field3name);
sqlite> .mode csv
sqlite> .headers ON
sqlite> .separator ,
sqlite> .import file.csv mytable

现在新表“mytable”包含文件中的数据,但第一行存储的是您通常不需要的标题,因此您需要删除它(在字段值周围使用单引号;如果您使用双引号,您将删除所有行):

sqlite> delete from mytable where field1name = 'field1name';

现在您可以轻松查看数据,按复杂公式过滤,按多个字段排序等。

sqlite> select * from mytable limit 30;

(抱歉,这变成了 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:

file.csv:
field1name, field2name, field3name
field1data, field2data, field3data
field1data, field2data, field3data

I create a new sqlite db (from the command line):

commandprompt> sqlite3 mynew.db

Then create a table in the db to import the file into:

sqlite> create table mytable (field1name, field2name, field3name);
sqlite> .mode csv
sqlite> .headers ON
sqlite> .separator ,
sqlite> .import file.csv mytable

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):

sqlite> delete from mytable where field1name = 'field1name';

Now you can easily look at the data, filter by complex formulas, sort by multiple fields, etc.

sqlite> select * from mytable limit 30;

(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).

謌踐踏愛綪 2024-10-26 20:18:10

您可能想看看 sc 作为替代方案。看看这个 linux 期刊页面

You probably want to look at sc as an alternative.. Have a look at this linux journal page

峩卟喜欢 2024-10-26 20:18:10

以下是在 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.

青衫负雪 2024-10-26 20:18:10

您可以使用 column< 在 CLI 中直接执行此操作,无需使用插件/a>:

column -s , -t input_file.csv > output_file.txt

请注意,某些命令选项可能会因您的 unix/linux 风格而异,因此请检查与您的风格相关的手册页。

You can do this right in the CLI without a plugin using column:

column -s , -t input_file.csv > output_file.txt

Note that some of the command options may differ on your flavor of unix/linux, so check the man page relevant to your flavor.

缱绻入梦 2024-10-26 20:18:10

我使用@chrisbra的插件,

" depending on your package manager
dein#add('chrisbra/csv.vim')

并在页面加载时添加了一个快速命令;
对于大型记录可能存在风险。

autocmd BufRead *.csv :%ArrangeColumn

I use @chrisbra's plugin,

" depending on your package manager
dein#add('chrisbra/csv.vim')

and I add a quick command on page load;
could be risky on large records.

autocmd BufRead *.csv :%ArrangeColumn
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文