001、 方法1
[email protected]:/home/test# ls a.fasta test.py [email protected]:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = [] else: dict1[key].append(i) dict1 = dict(sorted(dict1.items(), key = lambda x: x[0])) for i,j in dict1.items(): print(i) for k in j: print(k) in_file.close() [email protected]:/home/test# cat a.fasta ## 测试fasta文件 >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG [email protected]:/home/test# python test.py ## 执行程序 >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT
002、方法2
[email protected]:/home/test# ls a.fasta test.py [email protected]:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = [] else: dict1[key].append(i) for i in sorted(dict1): print(i) for k in dict1[i]: print(k) in_file.close() [email protected]:/home/test# cat a.fasta ## 测试fasta文件 >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG [email protected]:/home/test# python test.py ## 执行程序 >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT