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


    【问题】

    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文本中

  • 相关阅读:
    react状态管理工具redux的使用
    使用 vscode 插件可视化制作和管理脚手架及原理解析
    MySQL 数据库 增删查改、克隆、外键 等操作
    (附源码)基于Java SpringBoot的电影院管理系统设计与实现 毕业设计 011633
    精度误差问题与eps
    5G时代带动陶瓷PCB成长——GPS陶瓷天线调试方法(二)
    【开发问题&解决方法记录】01.dian
    成为钢铁侠!只需一块RTX3090,微软开源贾维斯(J.A.R.V.I.S.)人工智能AI助理系统
    Vue入门
    增删查改dom节点的操作
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126154626