是否有用于处理 csv 文件的强大命令行工具?

Ste*_*n D 59 command-line software-rec text-processing csv

我使用 CSV 文件,有时需要从命令行快速检查行或列的内容。在许多情况下cutheadtail、 和朋友会完成这项工作;然而, cut 不能轻易处理诸如

"this, is the first entry", this is the second, 34.5
Run Code Online (Sandbox Code Playgroud)

在这里,第一个逗号是第一个字段的一部分,但cut -d, -f1不同意。在我自己编写解决方案之前,我想知道是否有人知道已经存在用于这项工作的好工具。它至少必须能够处理上面的示例并从 CSV 格式的文件中返回一列。其他理想的功能包括能够根据第一行中给出的列名选择列、支持其他引用样式和支持制表符分隔的文件。

如果您不知道这样的工具,但对在 Bash、Perl 或 Python 或其他常见脚本语言中实现这样的程序有建议,我不会介意这样的建议。

小智 54

我可能有点太晚了,但还有另一个工具值得一提:csvkit

它有很多命令行工具可以:

  • 重新格式化 CSV 文件,
  • 从各种格式(JSON、SQL、XLS)与 CSV 相互转换,
  • 相当于cutgrepsort等人,但CSV感知,
  • 加入不同的CSV文件,
  • 对来自 CSV 文件的数据进行一般 SQL 查询。

  • 一个很好地满足问题标准的优秀工具(特别是它不需要跳入编程语言,并且精心设计以适应其他 Unix 实用程序)。 (6认同)

dog*_*ane 47

您可以使用 Python 的csv模块。

一个简单的例子:

import csv
reader = csv.reader(open("test.csv", "r"))
for row in reader:
    for col in row:
        print col
Run Code Online (Sandbox Code Playgroud)

  • 更好的是,使用 [Pandas](http://pandas.pydata.org/)。它明确设计用于处理表格数据。 (2认同)

Gil*_*il' 17

听起来像是 Perl 的一份工作Text::CSV

perl -MText::CSV -pe '
    BEGIN {$csv = Text::CSV->new();}
    $csv->parse($_) or die;
    @fields = $csv->fields();
    print @fields[1,3];
'
Run Code Online (Sandbox Code Playgroud)

请参阅有关如何处理列名的文档。可以使用参数调整分隔符和引用样式new。另请参阅Text::CSV::Separator分隔符猜测。

  • @ user7000,除非您的 shell 是 `(t)csh`,否则该命令在您的 shell 提示下可以正常工作。如果您希望将它们放在一行上,您始终可以将这些行连接在一起。换行符通常就像 C 中的 perl 语法中的空格一样。 (2认同)

小智 12

我找到了 csvfix,一个命令行工具可以很好地完成这项工作。但是,您需要自己制作:

http://neilb.bitbucket.org/csvfix

它可以完成您所期望的所有事情,排序/选择列,拆分/合并以及许多您不喜欢从 CSV 数据生成 SQL 插入和比较 CSV 数据的操作。


小智 10

如果你想使用命令行(而不是创建一个完整的程序来完成这项工作),你想使用rows,我正在研究的一个项目:它是表格数据的命令行界面,但也要在您的程序中使用的 Python 库。使用命令行界面,您可以使用简单的命令以 CSV、XLS、XLSX、HTML 或库支持的任何其他表格格式漂亮地打印任何数据:

rows print myfile.csv
Run Code Online (Sandbox Code Playgroud)

如果myfile.csv是这样的:

state,city,inhabitants,area
RJ,Angra dos Reis,169511,825.09
RJ,Aperibé,10213,94.64
RJ,Araruama,112008,638.02
RJ,Areal,11423,110.92
RJ,Armação dos Búzios,27560,70.28
Run Code Online (Sandbox Code Playgroud)

然后将以漂亮的方式打印内容,如下所示:

+-------+-------------------------------+-------------+---------+
| state |              city             | inhabitants |   area  |
+-------+-------------------------------+-------------+---------+
|    RJ |                Angra dos Reis |      169511 |  825.09 |
|    RJ |                       Aperibé |       10213 |   94.64 |
|    RJ |                      Araruama |      112008 |  638.02 |
|    RJ |                         Areal |       11423 |  110.92 |
|    RJ |            Armação dos Búzios |       27560 |   70.28 |
+-------+-------------------------------+-------------+---------+
Run Code Online (Sandbox Code Playgroud)

安装

如果您是 Python 开发人员并且已经pip在您的机器上安装了,只需在 virtualenv 中运行或使用sudo

pip install rows
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Debian:

sudo apt-get install rows
Run Code Online (Sandbox Code Playgroud)

其他很酷的功能

转换格式

您可以在任何支持的格式之间进行转换:

rows convert myfile.xlsx myfile.csv
Run Code Online (Sandbox Code Playgroud)

查询

是的,您可以将 SQL 用于 CSV 文件:

$ rows query 'SELECT city, area FROM table1 WHERE inhabitants > 100000' myfile.csv
+----------------+--------+
|      city      |  area  |
+----------------+--------+
| Angra dos Reis | 825.09 |
|       Araruama | 638.02 |
+----------------+--------+
Run Code Online (Sandbox Code Playgroud)

也可以使用--output参数将查询的输出转换为文件而不是标准输出。

作为 Python 库

您甚至还可以在 Python 程序中使用行:

import rows
table = rows.import_from_csv('myfile.csv')
rows.export_to_txt(table, 'myfile.txt')
# `myfile.txt` will have same content as `rows print` output
Run Code Online (Sandbox Code Playgroud)

希望你喜欢它!


小智 9

如果你想要终端中的可视化/交互工具,我竭诚推荐VisiData。

在此输入图像描述

它具有频率表(如上所示)、数据透视表、熔化图、散点图、使用 Python 进行过滤/计算等。

您可以像这样传递 csv 文件

vd hello.csv

有 csv 特定选项:--csv-dialect--csv-delimiter--csv-quotechar--csv-skipinitialspace用于微调 csv 文件的处理。


Ste*_*itt 8

Miller是另一个用于处理基于名称的数据的好工具,包括 CSV(带标题)。要提取 CSV 文件的第一列,而不关心其名称,您可以执行以下操作

printf '"first,column",second,third\n1,2,3\n' |
  mlr --csv --implicit-csv-header --headerless-csv-output cut -f 1
Run Code Online (Sandbox Code Playgroud)

  • 米勒的表现非常令人印象深刻。我将其与“awk”进行比较,但高度支持 DSV。 (2认同)

Fah*_*tha 7

R不是我最喜欢的编程语言,但它非常适合这样的事情。如果您的 csv 文件是

***********
foo.csv
***********
 col1, col2, col3
"this, is the first entry", this is the second, 34.5
'some more', "messed up", stuff
Run Code Online (Sandbox Code Playgroud)

在 R 解释器类型中

> x=read.csv("foo.csv", header=FALSE)

> x
                     col1                col2   col3
1 this, is the first entry  this is the second   34.5
2              'some more'           messed up  stuff
> x[1]  # first col
                      col1
1 this, is the first entry
2              'some more'
> x[1,] # first row
                      col1                col2  col3
1 this, is the first entry  this is the second  34.5
Run Code Online (Sandbox Code Playgroud)

关于您的其他请求,“能够根据第一行中给出的列名选择列”,请参阅

> x["col1"]
                      col1
1 this, is the first entry
2              'some more'
Run Code Online (Sandbox Code Playgroud)

对于“支持其他引用样式”,请参阅quoteread.csv(和相关函数)的参数。有关“对制表符分隔文件的支持”,请参阅sepread.csv的参数(设置sep为 '\t')。

有关详细信息,请参阅联机帮助。

> help(read.csv)
Run Code Online (Sandbox Code Playgroud)

  • 我对 R 非常熟悉,但我的目标是拥有可以从 Bash 轻松使用的东西。 (2认同)

小智 7

我使用了csvtool一次,它为我节省了很多时间和麻烦。可从外壳调用。

http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=447


小智 6

我推荐xsv,一个“用 Rust 编写的快速 CSV 命令行工具包”。

Ripgrep的作者撰写。

特色在我们如何使 CSV 处理速度提高 142 倍Reddit 线程)。


abo*_*uso 6

最好的工具之一是Miller。它就像 awk、sed、cut、join 和 sort 用于名称索引数据,例如 CSV、TSV 和表格 JSON。

例如

echo '"this, is the first entry", this is the second, 34.5' | \
mlr --icsv --implicit-csv-header cat
Run Code Online (Sandbox Code Playgroud)

给你

1=this, is the first entry,2= this is the second,3= 34.5
Run Code Online (Sandbox Code Playgroud)

如果你想要一个 TSV

echo '"this, is the first entry", this is the second, 34.5' | \
mlr --c2t --implicit-csv-header cat
Run Code Online (Sandbox Code Playgroud)

给你(可以删除标题)

1       2       3
this, is the first entry         this is the second      34.5
Run Code Online (Sandbox Code Playgroud)

如果您想要第一列和第三列,请更改它们的顺序

echo '"this, is the first entry", this is the second, 34.5' | \
mlr --csv --implicit-csv-header --headerless-csv-output cut -o -f 3,1
Run Code Online (Sandbox Code Playgroud)

给你

 34.5,"this, is the first entry"
Run Code Online (Sandbox Code Playgroud)