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

如何用Python读取指定文本行并自动生成数字转换脚本?

西班牙语口语化数字转阿拉伯数字脚本生成工具

需求说明

  • 读取number_info.txt文件,按规则提取两类数字:
    • 口语化数字:提取第0、3、6、9...行的内容,存入coloquial_numbers列表
    • 阿拉伯数字:提取第1、4、7、10...行的内容,存入symbolic_numbers列表
  • 基于上述列表自动生成number_to_text.py脚本,实现正则替换逻辑:
    • 若口语化数字包含空格,生成支持空格/连字符变体的正则规则(例如veinti tres对应veinti[\s|-]*tres)
    • 若口语化数字无空格,直接匹配原字符串完成替换

待完善代码

import re

def auto_coding_text_to_number():

    with open('number_info.txt', 'r') as f:

        #lines 0, 3, 6, 9, 12, 15, 18, ...
        coloquial_numbers = []

        #lines 0+1, 3+1, 6+1, 9+1, 12+1, 15+1, 18+1, ... 
        symbolic_numbers = []


    n = 0
    with open('number_to_text.py', 'w') as f:
        f.write('import re\n\ndef number_to_text_func(input_text):\n')
       
        #write replacement lines based on regex
        if(" " in coloquial_numbers[n]):
            #for example write this line:   "    input_text = re.sub(r\"veinti[\\s|-|]*tres\", \"23\", input_text)"
        
        if not (" " in coloquial_numbers[n]):
            #for example write this line:   "    input_text = re.sub(\"tres\", \"3\", input_text)"
            
        f.write("    return(input_text)\n    input_text = str(input())\n print(number_to_text_func(input_text))")

        n = n + 1

auto_coding_text_to_number()

期望生成的目标脚本

import re

def number_to_text_func(input_text):
    input_text = re.sub(r"veinti[\s|-]*tres", "23", input_text)
    input_text = re.sub(r"veinti[\s|-]*dos", "22", input_text)
    input_text = re.sub(r"veinti[\s|-]*uno", "21", input_text)
    input_text = re.sub("tres", "3", input_text)
    input_text = re.sub("dos", "2", input_text)
    input_text = re.sub("uno", "1", input_text)

    return(input_text)

input_text = str(input())
print(number_to_text_func(input_text))

完善后的代码

import re

def auto_coding_text_to_number():
    coloquial_numbers = []
    symbolic_numbers = []

    # 读取文件并按规则提取数字对
    with open('number_info.txt', 'r', encoding='utf-8') as f:
        lines = [line.strip() for line in f if line.strip()]
        # 每3行一组,取第0和1行作为数字对
        for i in range(0, len(lines), 3):
            if i + 1 < len(lines):
                coloquial_numbers.append(lines[i])
                symbolic_numbers.append(lines[i+1])

    # 生成目标脚本
    with open('number_to_text.py', 'w', encoding='utf-8') as f:
        f.write('import re\n\ndef number_to_text_func(input_text):\n')
        
        # 遍历数字对生成替换语句
        for coloquial, symbolic in zip(coloquial_numbers, symbolic_numbers):
            if " " in coloquial:
                regex_pattern = coloquial.replace(" ", r"[\s|-]*")
                f.write(f'    input_text = re.sub(r"{regex_pattern}", "{symbolic}", input_text)\n')
            else:
                f.write(f'    input_text = re.sub("{coloquial}", "{symbolic}", input_text)\n')
        
        # 写入返回逻辑和主程序
        f.write('    return input_text\n\ninput_text = str(input())\nprint(number_to_text_func(input_text))')

auto_coding_text_to_number()

关键优化点

  • 读取文件时过滤空行,避免无效数据干扰
  • 按每3行一组的规则批量提取数字对,适配指定行号要求
  • 用f-string简化代码生成逻辑,确保缩进和转义字符格式正确
  • 修正原代码中主程序逻辑写在函数内部的错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 04:55:22