XAN:用于终端的现代化 CSV 数据处理工具包
CSV 魔术师
License
Unlicense 和其他 2 个 license
912 stars 23 forks Branches Tags Activity
medialab/xan
master BranchesTags Go to file Code
Folders and files
Name| Name| Last commit message| Last commit date
---|---|---|---
Latest commit
History
2,118 Commits
.github/workflows| .github/workflows
docs| docs
scripts| scripts
src| src
tests| tests
.gitignore| .gitignore
CHANGELOG.md| CHANGELOG.md
CONTRIBUTING.md| CONTRIBUTING.md
COPYING| COPYING
Cargo.lock| Cargo.lock
Cargo.toml| Cargo.toml
LICENSE-MIT| LICENSE-MIT
README.md| README.md
UNLICENSE| UNLICENSE
clippy.toml| clippy.toml
View all files
Repository files navigation
xan,CSV 魔术师
xan 是一个命令行工具,可以直接从 shell 处理 CSV 文件。
它使用 Rust 编写,力求尽可能快、尽可能少地占用内存,并且可以轻松处理非常大的 CSV 文件(GB 级别)。它还能够利用并行性(通过多线程)使某些任务以计算机允许的最快速度完成。
它可以轻松地预览、过滤、切片、聚合、排序、连接 CSV 文件,并公开大量可组合的命令,这些命令可以链接在一起以执行各种典型的任务。
xan 还利用其自身的表达式语言,因此您可以执行复杂的任务,而这些任务无法通过依赖最简单的命令来完成。 这种极简的语言是为 CSV 数据量身定制的,并且比评估典型的动态类型语言(例如 Python、Lua、JavaScript 等)更快。
请注意,此工具最初是 BurntSushi 的 xsv 的一个分支,但此时几乎已被完全重写,以适应 SciencesPo's médialab 的用例,这些用例扎根于面向社会科学的 Web 数据收集和分析(您现在可能认为 CSV 已过时,但在过快判断之前阅读我们给该格式的 情书)。 因此,xan 超越了典型的数据操作,并公开了与词汇计量学、图论甚至抓取相关的实用程序。
最后,xan 可用于在终端中显示 CSV 文件,以便于浏览,甚至可用于绘制基本的数据可视化:
view command | flatten command
---|---
| 
categorical histogram | scatterplot
| 
categorical scatterplot | histograms
| 
parallel processing | time series
| 
small multiples (facet grid) | grouped view
| 
correlation matrix heatmap | heatmap
| 
概述
如何安装
Cargo
可以使用 cargo 安装 xan(通常附带 Rust):
cargo install xan
您还可以调整构建标志,以确保 Rust 编译器能够利用您 CPU 的所有功能:
CARGO_BUILD_RUSTFLAGS='-C target-cpu=native' cargo install xan
您还可以按以下方式安装最新的开发版本:
cargo install --git https://github.com/medialab/xan
Homebrew (macOS)
可以使用 Homebrew 在 macOS 上安装 xan,如下所示:
brew install xan
Arch Linux
您可以使用 pacman 从 extra repository 安装 xan:
sudo pacman -S xan
Nix
xan 已打包为 Nix,并且从 25.05 版本开始在 Nixpkgs 中可用。 要安装它,您可以将其作为 pkgs.xan 添加到您的 environment.systemPackages 中,或者使用 nix-shell 进入一个临时的 shell。
nix-shell -p xan
Pre-built binaries
预构建的二进制文件可以附加到每个 GitHub releases。 目前支持的目标包括:
x86_64-unknown-linux-muslx86_64-pc-windows-gnu
欢迎打开 PR 以通过添加相关目标来改进 CI。
安装 completions
请注意,xan 还公开了方便的自动 completions,用于命令和 header/column 名称,您可以通过 xan completions 命令安装这些 completions。
运行以下命令以了解如何安装这些 completions:
xan completions -h
快速入门
让我们通过探索法国媒体语料库来了解最常用的 xan 命令:
下载语料库
curl -LO https://github.com/medialab/corpora/raw/master/polarisation/medias.csv
显示文件头
xan headers medias.csv
0 webentity_id
1 name
2 prefixes
3 home_page
4 start_pages
5 indegree
6 hyphe_creation_timestamp
7 hyphe_last_modification_timestamp
8 outreach
9 foundation_year
10 batch
11 edito
12 parody
13 origin
14 digital_native
15 mediacloud_ids
16 wheel_category
17 wheel_subcategory
18 has_paywall
19 inactive
计算行数
xan count medias.csv
478
在终端中预览文件
xan view medias.csv
Displaying 5/20 cols from 10 first rows of medias.csv
┌───┬───────────────┬───────────────┬────────────┬───┬─────────────┬──────────┐
│ - │ name │ prefixes │ home_page │ … │ has_paywall │ inactive │
├───┼───────────────┼───────────────┼────────────┼───┼─────────────┼──────────┤
│ 0 │ Acrimed.org │ http://acrim… │ http://ww… │ … │ false │ <empty> │
│ 1 │ 24matins.fr │ http://24mat… │ https://w… │ … │ false │ <empty> │
│ 2 │ Actumag.info │ http://actum… │ https://a… │ … │ false │ <empty> │
│ 3 │ 2012un-Nouve… │ http://2012u… │ http://ww… │ … │ false │ <empty> │
│ 4 │ 24heuresactu… │ http://24heu… │ http://24… │ … │ false │ <empty> │
│ 5 │ AgoraVox │ http://agora… │ http://ww… │ … │ false │ <empty> │
│ 6 │ Al-Kanz.org │ http://al-ka… │ https://w… │ … │ false │ <empty> │
│ 7 │ Alalumieredu… │ http://alalu… │ http://al… │ … │ false │ <empty> │
│ 8 │ Allodocteurs… │ http://allod… │ https://w… │ … │ false │ <empty> │
│ 9 │ Alterinfo.net │ http://alter… │ http://ww… │ … │ <empty> │ true │
│ … │ … │ … │ … │ … │ … │ … │
└───┴───────────────┴───────────────┴────────────┴───┴─────────────┴──────────┘
在 unix 上,请不要犹豫使用 -p 标志来自动将完整输出转发到适当的寻呼机,并浏览所有列。
读取第一行的扁平表示形式
# NOTE: drop -c to avoid truncating the values
xan flatten -c medias.csv
Row n°0
───────────────────────────────────────────────────────────────────────────────
webentity_id 1
name Acrimed.org
prefixes http://acrimed.org|http://acrimed69.blogspot…
home_page http://www.acrimed.org
start_pages http://acrimed.org|http://acrimed69.blogspot…
indegree 61
hyphe_creation_timestamp 1560347020330
hyphe_last_modification_timestamp 1560526005389
outreach nationale
foundation_year 2002
batch 1
edito media
parody false
origin france
digital_native true
mediacloud_ids 258269
wheel_category Opinion Journalism
wheel_subcategory Left Wing
has_paywall false
inactive <empty>
Row n°1
───────────────────────────────────────────────────────────────────────────────
webentity_id 2
...
搜索行
xan search -s outreach internationale medias.csv | xan view
Displaying 4/20 cols from 10 first rows of <stdin>
┌───┬──────────────┬────────────────────┬───┬─────────────┬──────────┐
│ - │ webentity_id │ name │ … │ has_paywall │ inactive │
├───┼──────────────┼────────────────────┼───┼─────────────┼──────────┤
│ 0 │ 25 │ Businessinsider.fr │ … │ false │ <empty> │
│ 1 │ 59 │ Europe-Israel.org │ … │ false │ <empty> │
│ 2 │ 66 │ France 24 │ … │ false │ <empty> │
│ 3 │ 220 │ RFI │ … │ false │ <empty> │
│ 4 │ 231 │ fr.Sott.net │ … │ false │ <empty> │
│ 5 │ 246 │ Voltairenet.org │ … │ true │ <empty> │
│ 6 │ 254 │ Afp.com /fr │ … │ false │ <empty> │
│ 7 │ 265 │ Euronews FR │ … │ false │ <empty> │
│ 8 │ 333 │ Arte.tv │ … │ false │ <empty> │
│ 9 │ 341 │ I24News.tv │ … │ false │ <empty> │
│ … │ … │ … │ … │ … │ … │
└───┴──────────────┴────────────────────┴───┴─────────────┴──────────┘
选择一些列
xan select foundation_year,name medias.csv | xan view
Displaying 2 cols from 10 first rows of <stdin>
┌───┬─────────────────┬───────────────────────────────────────┐
│ - │ foundation_year │ name │
├───┼─────────────────┼───────────────────────────────────────┤
│ 0 │ 2002 │ Acrimed.org │
│ 1 │ 2006 │ 24matins.fr │
│ 2 │ 2013 │ Actumag.info │
│ 3 │ 2012 │ 2012un-Nouveau-Paradigme.com │
│ 4 │ 2010 │ 24heuresactu.com │
│ 5 │ 2005 │ AgoraVox │
│ 6 │ 2008 │ Al-Kanz.org │
│ 7 │ 2012 │ Alalumieredunouveaumonde.blogspot.com │
│ 8 │ 2005 │ Allodocteurs.fr │
│ 9 │ 2005 │ Alterinfo.net │
│ … │ … │ … │
└───┴─────────────────┴───────────────────────────────────────┘
排序文件
xan sort -s foundation_year medias.csv | xan view -s name,foundation_year
Displaying 2 cols from 10 first rows of <stdin>
┌───┬────────────────────────────────────┬─────────────────┐
│ - │ name │ foundation_year │
├───┼────────────────────────────────────┼─────────────────┤
│ 0 │ Le Monde Numérique (Ouest France) │ <empty> │
│ 1 │ Le Figaro │ 1826 │
│ 2 │ Le journal de Saône-et-Loire │ 1826 │
│ 3 │ L'Indépendant │ 1846 │
│ 4 │ Le Progrès │ 1859 │
│ 5 │ La Dépêche du Midi │ 1870 │
│ 6 │ Le Pélerin │ 1873 │
│ 7 │ Dernières Nouvelles d'Alsace (DNA) │ 1877 │
│ 8 │ La Croix │ 1883 │
│ 9 │ Le Chasseur Francais │ 1885 │
│ … │ … │ … │
└───┴────────────────────────────────────┴─────────────────┘
基于某些列对文件进行重复数据删除
# Some medias of our corpus have the same ids on mediacloud.org
xan dedup -s mediacloud_ids medias.csv | xan count && xan count medias.csv
457
478
也可以在排序时进行重复数据删除:
xan sort -s mediacloud_ids -u medias.csv
计算频率表
xan frequency -s edito medias.csv | xan view
Displaying 3 cols from 5 rows of <stdin>
┌───┬───────┬────────────┬───────┐
│ - │ field │ value │ count │
├───┼───────┼────────────┼───────┤
│ 0 │ edito │ media │ 423 │
│ 1 │ edito │ individu │ 30 │
│ 2 │ edito │ plateforme │ 14 │
│ 3 │ edito │ agrégateur │ 10 │
│ 4 │ edito │ agence │ 1 │
└───┴───────┴────────────┴───────┘
打印直方图
xan frequency -s edito medias.csv | xan hist
Histogram for edito (bars: 5, sum: 478, max: 423):
media |423 88.49%|━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━|
individu | 30 6.28%|━━━╸ |
plateforme | 14 2.93%|━╸ |
agrégateur | 10 2.09%|━╸ |
agence | 1 0.21%|╸ |
计算描述性统计信息
xan stats -s indegree,edito medias.csv | xan transpose | xan view -I
Displaying 2 cols from 14 rows of <stdin>
┌─────────────┬───────────────────┬────────────┐
│ field │ indegree │ edito │
├─────────────┼───────────────────┼────────────┤
│ count │ 463 │ 478 │
│ count_empty │ 15 │ 0 │
│ type │ int │ string │
│ types │ int|empty │ string │
│ sum │ 25987 │ <empty> │
│ mean │ 56.12742980561554 │ <empty> │
│ variance │ 4234.530197929737 │ <empty> │
│ stddev │ 65.07326792108829 │ <empty> │
│ min │ 0 │ <empty> │
│ max │ 424 │ <empty> │
│ lex_first │ 0 │ agence │
│ lex_last │ 99 │ plateforme │
│ min_length │ 0 │ 5 │
│ max_length │ 3 │ 11 │
└─────────────┴───────────────────┴────────────┘
评估表达式以过滤文件
xan filter 'batch > 1' medias.csv | xan count
130
要访问表达式语言的 cheatsheet,请运行 xan help cheatsheet。 要显示可用 functions 的完整列表,请运行 xan help functions。
评估表达式以基于其他列创建新列
xan map 'fmt("{} ({})", name, foundation_year)' key medias.csv | xan select key | xan slice -l 10
key
Acrimed.org (2002)
24matins.fr (2006)
Actumag.info (2013)
2012un-Nouveau-Paradigme.com (2012)
24heuresactu.com (2010)
AgoraVox (2005)
Al-Kanz.org (2008)
Alalumieredunouveaumonde.blogspot.com (2012)
Allodocteurs.fr (2005)
Alterinfo.net (2005)
要访问表达式语言的 cheatsheet,请运行 xan help cheatsheet。 要显示可用 functions 的完整列表,请运行 xan help functions。
通过评估表达式来转换列
xan transform name 'split(name, ".") | first | upper' medias.csv | xan select name | xan slice -l 10
name
ACRIMED
24MATINS
ACTUMAG
2012UN-NOUVEAU-PARADIGME
24HEURESACTU
AGORAVOX
AL-KANZ
ALALUMIEREDUNOUVEAUMONDE
ALLODOCTEURS
ALTERINFO
要访问表达式语言的 cheatsheet,请运行 xan help cheatsheet。 要显示可用 functions 的完整列表,请运行 xan help functions。
执行自定义聚合
xan agg 'sum(indegree) as total_indegree, mean(indegree) as mean_indegree' medias.csv | xan view -I
Displaying 1 col from 1 rows of <stdin>
┌────────────────┬───────────────────┐
│ total_indegree │ mean_indegree │
├────────────────┼───────────────────┤
│ 25987 │ 56.12742980561554 │
└────────────────┴───────────────────┘
要访问表达式语言的 cheatsheet,请运行 xan help cheatsheet。 要显示可用 functions 的完整列表,请运行 xan help functions。 最后,要显示可用 aggregation functions 的列表,请运行 xan help aggs。
对行进行分组并执行每组聚合
xan groupby edito 'sum(indegree) as indegree' medias.csv | xan view -I
Displaying 1 col from 5 rows of <stdin>
┌────────────┬──────────┐
│ edito │ indegree │
├────────────┼──────────┤
│ agence │ 50 │
│ agrégateur │ 459 │
│ plateforme │ 658 │
│ media │ 24161 │
│ individu │ 659 │
└────────────┴──────────┘
要访问表达式语言的 cheatsheet,请运行 xan help cheatsheet。 要显示可用 functions 的完整列表,请运行 xan help functions。 最后,要显示可用 aggregation functions 的列表,请运行 xan help aggs。
可用命令
[](https://