You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为含|符号的Description列数据添加引号包裹?

需求与解决方案

原始数据

Course_Number                   Description

10                             Lean Six Sigma - White Belt 2022 | Mar. 12-Abr
12                             Gold Six Sigma || Got it
13                             Diamond Six Sigma || Black Belt
13                             Six Lean Sigma

需求

当Description列内容包含|符号时,用双引号"包裹该列对应数据,最终输出如下:

Course_Number                   Description

10                             "Lean Six Sigma - White Belt 2022 | Mar. 12-Abr"
12                             "Gold Six Sigma || Got it"
13                             "Diamond Six Sigma || Black Belt"
13                             Six Lean Sigma

实现方法

方法1:命令行awk工具(适合快速处理文本文件)

执行以下命令:

awk 'NR<=2{print;next} /\|/{sub(/([[:space:]]+)(.*)/,"\\1\"\\2\"")}1' input.txt > output.txt
  • 说明:
    • NR<=2{print;next}:直接保留前两行(表头和空行)
    • /\|/{...}:匹配包含|的行,将第一个连续空格后的内容用双引号包裹
    • 执行后结果会写入output.txt

方法2:Excel/Google Sheets公式(表格软件环境)

在空白列(如C2单元格)输入公式,下拉填充:

=IF(ISNUMBER(SEARCH("|",B2)), """"&B2&"""", B2)
  • 说明:
    • 用SEARCH检测单元格是否包含|
    • 符合条件时,通过""""生成实际双引号并包裹内容,否则返回原内容

方法3:Python脚本(适合批量/自动化处理)

# 读取原始文件并处理
with open("input.txt", "r", encoding="utf-8") as infile, open("output.txt", "w", encoding="utf-8") as outfile:
    # 读取所有行并保留前两行表头和空行
    lines = infile.readlines()
    outfile.writelines(lines[:2])
    
    for line in lines[2:]:
        stripped_line = line.strip()
        if not stripped_line:
            outfile.write(line)
            continue
        # 分割课程号和描述(按第一个空格分隔)
        course_num, desc = stripped_line.split(maxsplit=1)
        if "|" in desc:
            # 重新拼接带引号的行,保持原格式对齐
            out_line = f"{course_num}                             \"{desc}\"\n"
        else:
            out_line = line
        outfile.write(out_line)

运行脚本后,处理结果会保存到output.txt。

内容的提问来源于stack exchange,提问作者SSA_Tech124

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:15:31