如何使用 CSVKit 截断列的长度?
定义如下:
这应该正确处理转义(引用)的列和新行。
例如:
First Header,Second Header
foo,
foo,b
foo,bar
foo,"bar"
foo,"""bar"
foo,"
bar"
Run Code Online (Sandbox Code Playgroud)
应该成为
First Header,Second Header
foo,
foo,b
foo,ba
foo,ba
foo,"""b"
foo,"
b"
Run Code Online (Sandbox Code Playgroud) 我搜索了这个任务,发现了以下较旧的问题:
但我无法使用,awk
因为我的数据是一个复杂的 CSV 文件,带有多个嵌套双引号。
假设我想对以下内容进行重复数据删除(简化情况):
Ref,xxx,zzz
ref1,"foo, bar, base",qux
ref1,"foo, bar, base",bar
ref2,aaa,bbb
Run Code Online (Sandbox Code Playgroud)
在输出中我需要它如下:
Ref,xxx,zzz
ref1,"foo, bar, base",qux
ref2,aaa,bbb
Run Code Online (Sandbox Code Playgroud)
没有awk
解决方案,只能使用任何 CSV 解析器。
我尝试了以下方法:
mlr --csv uniq -a -g Ref file.csv
Run Code Online (Sandbox Code Playgroud)
但这是一个错误。
Kusalananda 很好地建议使用csvformat
from csvkit格式化jq
@csv
为 csv 格式,不带双引号,"
回答如何使用 jq 解析 json。
这个答案似乎不涉及python的使用。但 csvkit安装教程及其安装故障排除似乎确实依赖于(也许需要)Python 的使用。这让我这个新手很困惑:
是否可以在不使用 python 的情况下在 git bash 中安装 csvkit(请参阅:打开spyder 或 anaconda,比方说)?如何?
编辑。MINGW64 (git bash) 显示bash: pip: command not found
. 对于 也一样conda
。您建议如何继续前进?
python 已安装,pip.exe 位于...\Anaconda\Scripts
. 其他站点上有几个建议的解决方案,例如以各种方式将 pip.exe 的目录添加到 PATH(此处和此处)。
我有一个 CSV 文件来实现这个效果:
+------------+--------------+
| Category I | Sub-Category |
+------------+--------------+
| 1144 | 128 |
| 1144 | 128 |
| 1000 | 100 |
| 1001 | 100 |
| 1002 | 100 |
| 1002 | 100 |
| 1011 | 102 |
| 1011 | 102 |
| 1011 | 102 |
| 1011 | 102 |
| 1011 | 102 |
| 1011 | 102 |
| 1013 | 103 |
| 1013 | 103 …
Run Code Online (Sandbox Code Playgroud)