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

如何从含空格换行的长字符串提取单词并生成表格格式输出

解决方法

方案1:Python 脚本(快速生成)

直接运行以下脚本,把需要处理的字符串粘贴到input_str里即可:

# 粘贴你的输入字符串(支持空格、换行)
input_str = """please i need help
another line with words
third line example"""

# 拆分所有单词(自动忽略空格、换行和空内容)
words = [word for word in input_str.split() if word]

# 生成目标格式内容
output = "\n".join([f"{{{i+1}, \"{word}\"}}," for i, word in enumerate(words)])

print(output)

运行后复制控制台输出,就是你需要的格式。

方案2:文本编辑器正则替换(无需编程)

以VS Code为例:

  1. 把输入字符串粘贴到编辑器中
  2. 按Ctrl+H打开替换面板,勾选「正则表达式」
  3. 查找框输入:(\w+)
  4. 替换框输入:{{$MATCH_NUMBER, "$1"}},"
  5. 点击「全部替换」,即可生成带序号的目标格式
    注:如果有空白行,先查找^\s*$替换为空,再执行上述步骤

方案3:命令行快速处理(Linux/macOS)

将输入内容保存为input.txt,执行以下命令:

awk '{for(i=1;i<=NF;i++) print "{"++cnt", \""$i"\"},"}' input.txt

也可以直接通过管道输入内容:

echo -e "your input string with spaces\nand newlines" | awk '{for(i=1;i<=NF;i++) print "{"++cnt", \""$i"\"},"}'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 00:50:28