001、方法1
(base) root@PC1:/home/test2# ls a.fasta test.py (base) root@PC1:/home/test2# cat a.fasta ## 测试fasta文件 >gene1 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene2 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene3 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG (base) root@PC1:/home/test2# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") idx = 0 for i in in_file: i = i.strip() if i[0] == ">": if idx > 0: print("") idx += 1 print(i.split(" ")[0].split(">")[1]) else: print(i, end = "") print("") in_file.close() (base) root@PC1:/home/test2# python test.py ## 执行程序 gene1 AGCTGCCTAAGCGGCATAGCTAATCG gene2 ACCGAATCGGAGCGATGGGCATTAAAGATCTAGCT gene3 AGGCTAGCGAGGCGCGAGGATTAGGCG
002、方法2
(base) root@PC1:/home/test2# ls a.fasta test.py (base) root@PC1:/home/test2# cat a.fasta ## 测试fasta文件 >gene1 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene2 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene3 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG (base) root@PC1:/home/test2# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = {} for i in in_file: i = i.strip() if i[0] == ">": key = i.split(" ")[0].split(">")[1] dict1[key] = "" else: dict1[key] += i for i,j in dict1.items(): print(i) print(j) in_file.close() (base) root@PC1:/home/test2# python test.py ## 执行程序 gene1 AGCTGCCTAAGCGGCATAGCTAATCG gene2 ACCGAATCGGAGCGATGGGCATTAAAGATCTAGCT gene3 AGGCTAGCGAGGCGCGAGGATTAGGCG
参考:https://www.jianshu.com/p/403a23fdd7bb