从命令行的csv文件中提取列。像cut或awk,但不会阻塞分隔符转义。
pluckr的Python项目详细描述
从命令行中的csv文件中提取列。像刀子或锥子,但没有 分隔符转义时阻塞。
安装
pip安装采摘器。完成。
用法
从stdin中获取列2和3(-f是一个索引):
pluckr -f 2,3 < sample1.csv
或直接从文件:
pluckr -f 2,1 sample1.csv
您请求的订单已被保留:
pluckr -f 1,2 sample1.csv != pluckr -f 2,1 sample1.csv
可以使用Python的负索引…获取第一个和最后一个列:
pluckr -f 1,-1 sample1.csv
您可以在列选择的范围内进行混合:
pluckr -f 2,4-6,9 sample1.csv
不能确定哪些列存在或它们的索引是什么?
pluckr –names sample1.csv
不存在的字段(例如
预先设定的行号可能会有帮助:
cat sample1.csv | pluckr –line-numbers
跳过标题行:
cat sample1.csv | pluckr -s1
抓取除第二列以外的所有列(删除列而不是选择它们):
pluckr -f 1 -i < sample2.csv
读取(和写入)各种分隔符:
pluckr -d, –out-delimiter=| < with-commas.csv > with-pipes.csv
请注意,将制表符作为参数传递可能会很困难;在必要时,请使用:
pluckr -d $’t’
帮助
通过–帮助:
- usage: pluckr [-h] [-f FIELDS] [-i] [-d DELIMITER] [-q QUOTECHAR] [-s SKIP]
- [-l] [-n] [infile]
Grab columns from csv input. http://github.com/philadams/pluckr
- positional arguments:
- infile input file (.csv)
- optional arguments:
-h, --help show this help message and exit -f FIELDS ordered list of columns to retain; one-indexed -i, --inverse invert column retention: drop those in -f -d DELIMITER, --delimiter DELIMITER field delimiter when reading infile -q QUOTECHAR, --quotechar QUOTECHAR field quotechar when reading infile -s SKIP, --skip SKIP number of rows to skip -l, --line-numbers prepend line numbers to output -n, --names print column names; assumes one header row
测试
这个项目使用[roundup](https://github.com/bmizerany/roundup)进行测试。 使用make test运行测试。
未来
- 允许-f在有标题行时按名称获取列?
- implement–out-x用于输出分隔符等
- json输出或者,为kennethreitz/tablib贡献一个cli
- 添加分隔符支持
- 添加QuoteChar支持