全部版块 我的主页
› 论坛 › 人工智能 › 人工智能
269 0
2026-10-13
<!-- markdown css tag --><section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="; color: black; padding: 0 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: PingFangSC-Light;"><figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/5f11e607-5523-4ed5-b174-8cfef1aa33b2.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">引言</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">几乎每个数据工作流都会遇到 CSV 文件。来自数据库、应用和批处理任务的导出,最终往往以文件形式落地,同时伴随各种问题:分隔符不一致、编码错误、表结构变化、重复行。这些问题通常都不大,但处理过程重复枯燥,在时间压力下容易出错,而且很少值得专门开发定制工具。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">本文介绍五个常见的 CSV 处理任务,每个任务都提供自包含的 Python 脚本。所有脚本仅依赖 Python 标准库,无需安装第三方包或管理额外依赖,直接就能运行。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">配套代码仓库:<a href="https://github.com/balapriyac/data-science-tutorials/tree/main/useful-python-sc ripts-csv-processing" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">balapriyac/data-science-tutorials</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">1. 结构校验器(Schema Validator)</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">痛点</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">一个在电子表格预览里"看起来没问题"的 CSV,实际可能缺少必需列、日期字段塞满文本、或者数字列混入了空白字符串。这些问题通常要到下游——也就是真正消费这份文件的系统——才暴露出来,排查起来代价高昂。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">脚本功能</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">对照你定义的结构(Schema)检查 CSV:必需列、预期数据类型,以及"不得为空""必须匹配某个正则"这类简单约束。输出逐行错误报告,而不是简单的通过/不通过结论,让你能精确定位哪些单元格违反了哪条规则。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">实现原理</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">结构定义放在一个小型 JSON 文件中:每一列映射到一种类型(<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">int</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">float</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">date</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">string</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">email</code> 等),可选一个正则模式,并标注是否必需。脚本用 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">csv.DictReader</code> 逐行流式读取 CSV,因此即便文件很大也能处理,不会把全部数据载入内存;逐列应用每条规则,并收集带行号与列名的失败记录。校验失败时脚本以非零状态码退出,方便作为流水线中的关卡,在数据进一步向下游流转前拦截问题。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">代码:<a href="https://github.com/balapriyac/data-science-tutorials/blob/main/useful-python-sc ripts-csv-processing/csv_schema_validator.py" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">csv_schema_validator.py</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">2. 行级差异对比工具(Row-Level Diff Tool)</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">痛点</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">对比同一 CSV 的两个版本——比如昨天的导出和今天的导出,或源文件与落入数据库的数据——通常意味着把两份电子表格并排翻阅。随着行数增多,这种方式越来越难以管理,也更容易漏掉变化。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">脚本功能</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">使用你指定的主键列(或组合列)对比两个 CSV 文件,报告哪些行是新增、哪些被删除、哪些发生了字段级变更。完全未变化的行会被忽略,输出聚焦在真正变动的部分。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">实现原理</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">两个文件都读入以标识列为键的字典。脚本用集合差集找出新增和删除的键;对于两个文件都存在、且已变化的行,逐列比较取值,只记录不同的列及其新旧值。输出写入一份 CSV 报告,包含 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">change_type</code>(变更类型)、主键、列名、旧值、新值,方便审阅者过滤或排序。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">代码:<a href="https://github.com/balapriyac/data-science-tutorials/blob/main/useful-python-sc ripts-csv-processing/csv_diff_tool.py" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">csv_diff_tool.py</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">3. 编码与分隔符规范化工具(Encoding &amp; Delimiter Normalizer)</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">痛点</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">并非所有 CSV 都是逗号分隔,也并非所有 CSV 都是 UTF-8。来自老旧系统的文件可能使用分号、制表符,或者带有会破坏第一列表头的字节序标记。任何一种情况都会导致下游工具要么直接报错,要么在毫无提示的情况下解析错误。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">脚本功能</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">检测输入文件的分隔符和字符编码,然后重写为干净的、UTF-8、逗号分隔的 CSV。去除字节序标记、规范换行符,并报告检测与修改的内容。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">实现原理</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">脚本以二进制模式读取文件样本,尝试一串常见的候选编码,如果都解码不干净,则回退到字节级启发式判断。<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">csv.Sniffer</code> 检测解码后文本的样本,在逗号、分号、制表符、竖线之间猜测分隔符。随后用检测到的设置重新读取文件,并以 Python 默认的 CSV 方言重写,即逗号分隔、UTF-8 编码、统一换行符。控制台输出的简短摘要记录原始编码与分隔符,让变更可追溯。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">代码:<a href="https://github.com/balapriyac/data-science-tutorials/blob/main/useful-python-sc ripts-csv-processing/csv_normalizer.py" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">csv_normalizer.py</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">4. 可配置列转换器(Configurable Column Transformer)</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">痛点</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">重命名列、调整顺序、删除不需要的列、从现有列派生新列(比如合并姓氏与名字、把货币字符串转成浮点数),这些操作对单个文件用电子表格很容易完成。但当需要跨几十个文件一致地执行,或每次新导出到达都要重复操作时,就值得自动化了。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">脚本功能</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">应用配置文件中定义的一组列操作:重命名(rename)、删除(drop)、重排(reorder)、派生(derive)。派生列基于一套小而安全的表达式语法构建,而不是任意代码,因此配置文件保持可读,也不必信任任意 Python 执行。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">实现原理</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">配置文件是一份按顺序处理的 JSON 操作列表。<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">rename</code> 和 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">drop</code> 分别对应字典与列表操作;<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">derive</code> 接受新列名和类似 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">{first_name} {last_name}</code> 或 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">{price_str}</code> 的模板字符串,并在之后应用注册好的转换函数,如 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">to_float</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">to_int</code>、<code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">strip_currency</code>。脚本用 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">csv.DictReader</code> 与 <code style="; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">csv.DictWriter</code> 逐行处理,内存占用恒定不随文件大小增长,并严格按配置指定的顺序写出最终列顺序。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">代码:<a href="https://github.com/balapriyac/data-science-tutorials/blob/main/useful-python-sc ripts-csv-processing/csv_column_transformer.py" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">csv_column_transformer.py</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">5. 采样器与字段匿名化工具(Sampler &amp; Field Anonymizer)</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">痛点</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">分享一份生产数据的切片——给同事、随工单附上、或用于测试环境——要么发整个文件,要么在电子表格里手动涂掉敏感列。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">脚本功能</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">从大型 CSV 中随机抽取一部分行,并对你标记为敏感的任何列,把真实值替换成一致的、不可逆的占位符——同一输入值在单次运行中总产生相同的脱敏输出,因此行与行之间的关系得以保留,同时不会暴露原始数据。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;"><strong style="font-weight: border; color: rgb(248,57,41);">实现原理</strong></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">使用蓄水池采样(reservoir sampling)从文件中抽取随机、均匀的行样本,无需先整体载入内存,这对超大文件很重要。对于配置中标记为敏感的每一列,脚本对原始值应用带密钥的哈希,并截断为简短、可读的令牌。例如,邮箱地址会被替换为一致的假名化值,行间的引用关系得以保留,而不保留原始值。摘要行报告抽了多少行、掩码了哪些列。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">代码:<a href="https://github.com/balapriyac/data-science-tutorials/blob/main/useful-python-sc ripts-csv-processing/csv_sampler_anonymizer.py" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">csv_sampler_anonymizer.py</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; ; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">收尾总结</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; ; word-spacing: 0.1em;">这五个脚本覆盖了经常出现、却又不足以每次都从零编写专用代码的 CSV 杂活。挑一个匹配当前任务的脚本,直接上手用就行。</p>
<section class="table-container" data-tool="mdnice编辑器" style="overflow-x: auto;"><table style="display: table; text-align: left;">
<thead>
<tr style="border: 0; border-top: 1px solid #ccc; background-color: white;">
<th style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0; ; min-;">脚本名称</th>
<th style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0; ; min-;">用途</th>
<th style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0; ; min-;">核心特性</th>
<th style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0; ; min-;">最佳使用场景</th>
</tr>
</thead>
<tbody style="border: 0;">
<tr style="border: 0; border-top: 1px solid #ccc; background-color: white;">
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">结构校验器</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">对照定义好的结构检查 CSV</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">类型检查、正则模式、逐行错误报告</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">数据进入流水线前的把关拦截</td>
</tr>
<tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;">
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">行级差异对比工具</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">对比两个 CSV 快照</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">基于主键匹配、字段级变更细节</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">审计各次运行之间的导出</td>
</tr>
<tr style="border: 0; border-top: 1px solid #ccc; background-color: white;">
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">编码与分隔符规范化</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">统一不一致的 CSV 格式</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">编码检测、分隔符嗅探、BOM 剥离</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">清理来自老旧系统的文件</td>
</tr>
<tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;">
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">列转换器</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">重命名、删除、重排、派生列</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">配置驱动、安全表达式语法、流式处理</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">跨多个文件重复同样的重塑</td>
</tr>
<tr style="border: 0; border-top: 1px solid #ccc; background-color: white;">
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">采样器与匿名化</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">抽样行并掩码敏感字段</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">蓄水池采样、一致的密钥哈希</td>
<td style="border: 1px solid #ccc; padding: 5px 10px; text-align: left; ; min-;">安全地分享真实数据</td>
</tr>
</tbody>
</table>
</section><h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ;"><span class="prefix" style="display: none;"></span><span class="content" style="; color: rgb(165,213,93);">推荐学习书籍 《<strong style="font-weight: border; color: rgb(248,57,41);">CDA一级教材</strong>》适合CDA一级考生备考,也适合业务及数据分析岗位的从业者提升自我。完整电子版已上线CDA网校,累计已有10万+在读~ !</span><span class="suffix" style="display: none;"></span></h3>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/4502d41c-fb3a-43a8-bf50-9e14aedde094.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; ;"><span class="prefix" style="display: none;"></span><span class="content" style="; color: rgb(165,213,93);">免费加入阅读:<a href="https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0</a></span><span class="suffix" style="display: none;"></span></h3>
</section>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群