Excel的批量替换的痛点在于只能每次单次替换一个,利用脚本可以批量同时替换多个字符。首先需要建立replace_new.txt写入你所需要替换的文本,如:
“origin new
A1 B1
B2 B2
C1 C2”
#使用Table空格,换行使用Enter。
脚本如下:
nameE = {}
for line in open("replace_new.txt", "r", encoding="utf-8"):
lineL = line.strip().split("\t")
origin_name = lineL[0]
new_name = lineL[1]
nameE[origin_name] = new_name
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
with open("target_txt.txt", "r", encoding="utf-8") as target_text, open("target_new.fasta", "w", encoding="utf-8") as new_file1:
target_text = target_text.read() #打开文件target_text后需要读取文本后才能使用
a = replace_all(target_text, nameE)
new_file1.write(a)
网友评论