Python 批量键码短语转换脚本
在使用五笔输入法或者自定义短语时,经常需要把“短语 + 键码 + 数量”的格式,批量转换成“键码,数量=短语”的格式,或者反向还原。本文提供一款 双向批量转换 Python 脚本,适合处理大量短语记录。
功能说明
- 正向转换(Forward)
原始格式:
FIX
- 反向转换(Reverse)
原始格式:
FIX
转换后格式:
PYTHON
脚本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| """ 双向批量转换脚本 支持: 1. 正向转换:短语+键码+数量 -> 键码,数量=短语 2. 反向转换:键码,数量=短语 -> 短语+键码+数量 """
import os
input_file = "input.txt" output_file = "output.txt" mode = "forward"
with open(input_file, "r", encoding="utf-8") as f: lines = f.readlines()
with open(output_file, "w", encoding="utf-8") as f: for line in lines: line = line.strip() if not line: continue try: if mode == "forward": parts = line.rsplit("\t", 2) if len(parts) < 3: print(f"警告:无法解析此行 -> {line}") continue short_phrase = parts[0] key_code = parts[1] number = parts[2] f.write(f"{key_code},{number}={short_phrase}\n") elif mode == "reverse": key_num, short_phrase = line.split("=", 1) key_code, number = key_num.split(",", 1) f.write(f"{short_phrase}\t{key_code}\t{number}\n") else: raise ValueError("模式错误,请使用 'forward' 或 'reverse'") except Exception as e: print(f"处理行出错: {line}\n错误: {e}")
print(f"转换完成,结果已保存到 {output_file}")
|
使用说明
- 准备输入文件
NODE-REPL1 2 3
| 短语1 键码1 数量1 短语2 键码2 数量2 ...
|
ROUTEROS1 2 3
| 键码1,数量1=短语1 键码2,数量2=短语2 ...
|
- 修改模式
PYTHON1 2
| mode = "forward" mode = "reverse"
|
- 运行脚本
Windows:
BASH
Mac / Linux:
BASH1
| python3 batch_convert.py
|
- 查看输出
- 输出文件为
output.txt
,根据模式生成对应内容。
- 准备文件
- 脚本仅使用 Python 标准库,无需安装额外库。
- 将
batch_convert.py
和 input.txt
放在同一个目录下。
- 修改模式
mode = "forward"
或 mode = "reverse"
。
示例
原始短语示例(正向):
ROUTEROS1 2
| 高级蓝牙加按键版(全国版,可按键操作) trww 3 特价款(单城市) twwffi 3
|
正向转换后输出:
1 2
| trww,3=高级蓝牙加按键版(全国版,可按键操作) twwffi,3=特价款(单城市)
|
反向转换后输出:
BASH1 2
| 高级蓝牙加按键版(全国版,可按键操作) trww 3 特价款(单城市) twwffi 3
|
新电脑运行环境与依赖
- 安装 Python
- 下载:Python 官网
- 建议安装 Python 3.10+
- Windows:勾选 “Add Python to PATH”
- Mac:用 Homebrew 安装
brew install python
- Linux:使用系统包管理器
sudo apt install python3 python3-pip
验证安装:
1 2 3
| python --version
python3 --version
|
- 安装依赖
- 脚本仅使用 Python 标准库,无需安装额外库。
- 准备文件
- 将
batch_convert.py
和 input.txt
放在同一个目录下。
- 修改模式
mode = "forward"
或 mode = "reverse"
。
- 运行脚本
BASH1 2
| python batch_convert.py python3 batch_convert.py
|
- 查看输出
- 输出文件为
output.txt
,根据模式生成对应内容。
- 小技巧
小技巧(Tips)
- Windows:直接把输入文件拖到脚本同目录,运行即可。
- Mac/Linux:终端进入脚本目录,运行
python3 batch_convert.py
。
- 支持批量处理上百行甚至上千行短语记录,非常适合自定义输入法或整理键码短语。