python处理txt文本文件

现有output.txt文件如下图

img


一列是TCGA开头,一列分为Primary Tumor或Solid Tissue Normal,两列用\t分隔
现需要生成两个列,长度为output.txt的行数,并储存到A1.txt和A2.txt中
其中A1的内容为Primary Tumor对应字母C,Solid Tissue Normal对应字母N,输出内容仅含C或N,用\t分隔
A2的内容为Primary Tumor对应1,Solid Tissue Normal对应数字0,输出内容仅含0或1的,用\t分隔

这个代码是对的给他点赞

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')