本文目录导读:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
文本拼接工具 - 支持多种拼接模式
功能:将多个文本片段按照指定分隔符或规则进行拼接
"""
import sys
import os
from typing import List, Optional
class TextJoiner:
"""文本拼接器"""
@staticmethod
def join_with_delimiter(texts: List[str], delimiter: str = "") -> str:
"""
使用分隔符拼接文本列表
Args:
texts: 文本列表
delimiter: 分隔符,默认为空字符串
Returns:
拼接后的字符串
"""
return delimiter.join(texts)
@staticmethod
def join_by_line(texts: List[str], line_ending: str = "\n") -> str:
"""
按行拼接,每行一个文本
Args:
texts: 文本列表
line_ending: 行尾符,默认为换行符
Returns:
拼接后的多行字符串
"""
return line_ending.join(texts)
@staticmethod
def join_with_template(texts: List[str], template: str = "{text}") -> str:
"""
使用模板拼接每个文本,并连接起来
Args:
texts: 文本列表
template: 模板字符串,{text} 会被替换为原始文本
Returns:
拼接后的字符串
"""
results = [template.format(text=t) for t in texts]
return "".join(results)
@staticmethod
def read_file(filepath: str) -> List[str]:
"""
从文件读取文本行(去除空白行)
Args:
filepath: 文件路径
Returns:
文本行列表
"""
if not os.path.exists(filepath):
raise FileNotFoundError(f"文件不存在: {filepath}")
with open(filepath, 'r', encoding='utf-8') as f:
return [line.strip() for line in f if line.strip()]
@staticmethod
def interactive_mode() -> None:
"""交互式模式:用户逐行输入文本,实时显示拼接结果"""
print("=== 交互式文本拼接模式 ===")
print("输入文本行(输入空行结束):")
lines = []
while True:
line = input()
if not line:
break
lines.append(line)
if not lines:
print("未输入任何文本。")
return
print("\n--- 拼接结果 ---")
print("1. 无分隔符拼接:", TextJoiner.join_with_delimiter(lines))
print("2. 逗号分隔:", TextJoiner.join_with_delimiter(lines, ","))
print("3. 空格分隔:", TextJoiner.join_with_delimiter(lines, " "))
print("4. 换行输出:")
print(TextJoiner.join_by_line(lines))
print("5. 模板拼接([文本]):", TextJoiner.join_with_template(lines, "[{text}]"))
def batch_process_file(input_file: str, output_file: str,
delimiter: str = "", by_line: bool = False) -> None:
"""
批量处理文件
Args:
input_file: 输入文件路径
output_file: 输出文件路径
delimiter: 分隔符
by_line: 是否按行输出(否则拼接为一行)
"""
try:
texts = TextJoiner.read_file(input_file)
if by_line:
result = TextJoiner.join_by_line(texts)
else:
result = TextJoiner.join_with_delimiter(texts, delimiter)
with open(output_file, 'w', encoding='utf-8') as f:
f.write(result)
print(f"处理完成!结果已保存至: {output_file}")
except Exception as e:
print(f"处理出错: {e}", file=sys.stderr)
def main():
"""主函数 - 命令行入口"""
import argparse
parser = argparse.ArgumentParser(
description="文本拼接工具 - 将多个文本片段按指定方式拼接",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例用法:
%(prog)s -f input.txt -o output.txt # 无分隔符拼接
%(prog)s -f input.txt -d "," -o output.txt # 逗号分隔
%(prog)s -f input.txt -l # 按行输出
%(prog)s -i # 交互模式
"""
)
# 输入方式
input_group = parser.add_mutually_exclusive_group()
input_group.add_argument("-f", "--file", help="输入文件路径")
input_group.add_argument("-i", "--interactive", action="store_true",
help="交互模式")
# 拼接选项
parser.add_argument("-o", "--output", help="输出文件路径(文件模式必需)")
parser.add_argument("-d", "--delimiter", default="",
help="分隔符,默认为空字符串")
parser.add_argument("-l", "--line-by-line", action="store_true",
help="按行输出(每个文本占一行)")
parser.add_argument("-t", "--template",
help="使用模板拼接,'{text} '")
args = parser.parse_args()
# 交互模式
if args.interactive:
TextJoiner.interactive_mode()
return
# 文件模式
if args.file:
if not args.output:
parser.error("文件模式需要指定输出文件 (-o)")
if args.template:
# 使用模板模式
try:
texts = TextJoiner.read_file(args.file)
result = TextJoiner.join_with_template(texts, args.template)
with open(args.output, 'w', encoding='utf-8') as f:
f.write(result)
print(f"模板拼接完成!结果已保存至: {args.output}")
except Exception as e:
print(f"处理出错: {e}", file=sys.stderr)
else:
batch_process_file(args.file, args.output,
args.delimiter, args.line_by_line)
else:
parser.print_help()
if __name__ == "__main__":
main()
使用方法
交互模式(推荐测试用)
python text_joiner.py -i
然后逐行输入文本,输入空行结束,自动显示多种拼接结果。
文件批处理模式
# 准备输入文件 input.txt,每行一个文本片段
echo "hello" > input.txt
echo "world" >> input.txt
echo "python" >> input.txt
# 无分隔符拼接
python text_joiner.py -f input.txt -o output.txt
# 逗号分隔
python text_joiner.py -f input.txt -d "," -o output.txt
# 按行输出(保持原样)
python text_joiner.py -f input.txt -l -o output.txt
# 模板拼接
python text_joiner.py -f input.txt -t "[{text}]" -o output.txt
作为模块导入
from text_joiner import TextJoiner
texts = ["Hello", "World", "Python"]
print(TextJoiner.join_with_delimiter(texts, ","))
print(TextJoiner.join_with_template(texts, "{text}\n"))
功能特性
| 模式 | 命令参数 | 说明 |
|---|---|---|
| 无分隔符拼接 | 默认 | 所有文本直接连在一起 |
| 自定义分隔符 | -d "," |
用指定字符分隔文本 |
| 按行输出 | -l |
每行一个文本 |
| 模板拼接 | -t "[{text}]" |
使用模板包裹每个文本 |
| 交互模式 | -i |
手动输入内容测试 |
这个脚本灵活且实用,可以根据您的具体需求调整拼接规则。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播