现有output.txt文件如下图
这个代码是对的给他点赞
def main():
with open("output.txt", "r") as f1:
data1 = f1.read()
data2 = data1.replace("Primary Tumor", "C").replace("Solid Tissue Normal", "N")
with open("A1.txt", "w") as f2:
f2.write(data2)
data3 = data1.replace("Primary Tumor", "1").replace("Solid Tissue Normal", "0")
with open("A2.txt", "w") as f3:
f3.write(data3)
if __name__ == '__main__':
main()
如果只是为了得到文件的话不必这么复杂,在记事本中查找替换对应的字母或数字即可
def main():
with open("output.txt", "r") as f1:
data1 = f1.read()
data2 = data1.replace("Primary Tumor", "C").replace("Solid Tissue Normal", "N")
with open("A1.txt", "w") as f2:
f2.write(data2)
data3 = data1.replace("Primary Tumor", "1").replace("Solid Tissue Normal", "0")
with open("A2.txt", "w") as f3:
f3.write(data3)
if __name__ == '__main__':
main()
#srcFile 源文件output.txt
#destFile1 目标文件A1.txt
#destFile1 目标文件A2.txt
def fileProcess(srcFile, destFile1, destFile2):
with open(srcFile, "r") as fsrc:
data1 = fsrc.read()
data2 = data1.replace("Primary Tumor", "C").replace("Solid Tissue Normal", "N")
with open(destFile1, "w") as fdest1:
fdest1.write(data2)
data3 = data1.replace("Primary Tumor", "1").replace("Solid Tissue Normal", "0")
with open(destFile2, "w") as fdest2:
fdest2.write(data3)
fdest1.close()
fdest2.close()
fsrc.close()
if __name__ == '__main__':
fileProcess('output.txt', 'A1.txt', 'A2.txt')