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


    【问题】

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

  • 相关阅读:
    6.0、C语言——递归函数
    qtcreator调试webkit
    【leetcode刷题记录】js-两数之和
    图像识别技术:提升零售行业的顾客体验与运营效率
    流畅的python
    有哪些编程语言能在AI的应用上大显身手?
    Linux操作系统编程基础
    JAVA面试技巧之项目介绍
    自定义GPT已经出现,并将影响人工智能的一切,做好被挑战的准备了吗?
    HelloWorld:通过demo,构建黑盒模型
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126154626