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

【问题】

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值