[Python] 纯文本查看 复制代码 import re
# 示例文本
texts = [
"拼命苟活的第一天:",
"拼命苟活的第二天:",
"拼命苟活的第三天:",
"拼命苟活的第四天:",
"拼命苟活的第五天:",
"营业第1招",
"营业第2招",
"营业第3招",
"营业第4招",
"营业第5招"
]
# 中文数字到阿拉伯数字的映射
chinese_to_arabic = {
"一": "1",
"二": "2",
"三": "3",
"四": "4",
"五": "5"
}
# 定义替换函数
def replace_chapter_name(text):
# 匹配并替换中文数字
text = re.sub(r'第(一|二|三|四|五)', lambda m: "第{}章".format(chinese_to_arabic[m.group(1)]), text)
# 匹配并替换阿拉伯数字
text = re.sub(r'第(\d)', r'第\1章', text)
return text
# 应用替换
updated_texts = [replace_chapter_name(text) for text in texts]
for text in updated_texts:
print(text)
|