• 批量替换文本中的多组字符串


    【问题】

    hi friends, I have the following demand and I want to know if you have any suggestions for me:
    I want to REPLACE certain strings from one text file with corresponding strings stored in another text file (one to one matching), and save to a new text file (override the original file is also acceptable).
    I want to know what is the best programming language to handle this task? all opinions are welcome and appreciated.
    If I only had limited knowledge of C programming, html, php, would you list Python as a recommendation for me to handle this problem?
    Thank you all.

    【回答】

           要想批量替换多组字符串,python用两层循环可以实现,对于初学者来说代码比较难写。可以考虑SPL,不需要循环就能实现,脚本如下:

    A
    1=file("e:\\condition.txt").import@t()
    2=A1.iterate(replace(~~,before,after),file("e:\\source.txt").read())
    3=file("e:\\result.txt").write(A2)

    A1:读取condition.txt文本,返回序表,其中有before列和after列

    A2:循环A1,将file("e:\\source.txt").read()字符串中的某些子串批量替换成其他字符串,待替换的子串就是before列的字符串,替换的子串为after列对应的字符串

    A3:将批量替换完的字符串结果写入result.txt文本中

           如果文件太大无法放入内存,那不论是python还是c都很难用双层循环简单实现了,代码会更加复杂。集算器提供游标读取大文件,易于实现批量替换,具体脚本如下:

    AB
    1=file("e:\\condition.txt").import@t()
    2=file("e:\\source.txt").cursor@s()
    3for A2,1000
    4=A3.(_1).concat(“\r\n”)
    5=A1.iterate(replace(~~,before,after),B4)
    6=file("e:\\result.txt").write@a(B5)

    A1:读取condition.txt文本,返回序表,其中有before列和after列

    A2:读取source.txt文本,返回游标,游标内容为单字段串序表

    A3:对A2游标循环分段读取,每次读取1000条记录

    B4:读取_1列列值,并按照换行分隔符拼成一个字符串

    B5:对A1循环,将B4字符串中的某些子串批量替换成其他字符串

    待替换的子串就是before列的字符串,替换的子串为after列对应的字符串

    B6:把每次替换完的字符串B5追加写入result.txt文本中

  • 相关阅读:
    Mybatis04关联关系映射
    react native中使用Animated实现三张图片动态旋转效果
    【图神经网络 01】
    华东交通大学2022年ACM“双基“程序设计竞赛
    四旋翼无人机学习第3节--cadence原理图库创建以及基础元器件绘制
    Focal and Global Knowledge Distillation for Detectors
    RabbitMQ消息丢失、消息重复消费、消息顺序性无法保证、消息积压、一致性问题、系统可用性降低等这些常见问题怎么解决
    哈希表学习
    acwing第 126 场周赛 (扩展字符串)
    数据结构与算法之一道题感受算法(算法入门)
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126154626