python if if if not command usage

时间:2017-05-11 13:26:17

标签: python

#!/usr/bin/python
import os
import re
path=os.getcwd()
list_dir=os.listdir(path+'//Osativa')
if not os.path.exists('results'):
    os.makedirs('results') 
fo1=open('./results/annotation.txt','w')
f1=open(path+'//CLEAN FILE.csv').readlines()
f2=open(path+'//Osativa//Osativa_323_v7.0.annotation_info.txt').readlines()
#num=0
for line in f1:
    locus_id=line.split(',')[1]
    if locus_id.startswith('LOC'):
        #num +=1
        #print(num)
        #print (locus_id)
        #num1=0
        for line1 in f2:
            if locus_id==line1.split()[1]:
                #num1+=1
                #print (num1)
                #print locus_id
                transcript=line1.split()[2]
                if transcript.startswith('LOC'):
                    #print (locus_id+'\t'+transcript)
                    for i in line1.split():
                         if i.startswith('PF'):
                              print (locus_id+'\t'+transcript+'\t'+i)
                              if not i.startswith('PF'):
                                  print (locus_id+'\t'+transcript+'\t'+'\t')

我的问题是,如果特定基因没有PF id,我想在那里键入tab键(保留为空格)。那我该怎么做呢? 此外,我想将此输出转换为excel文件,因此如果某些基因没有PF id,我可以在那里看到一个空格。我想我的问题可以理解。最后,我想将输出转换为另一个txt文件

1 个答案:

答案 0 :(得分:1)

你有缩进问题,最好在这里使用else block

for line in f1:
    locus_id = line.split(',')[1]
    if locus_id.startswith('LOC'):
        # num +=1
        # print(num)
        # print (locus_id)
        # num1=0
        for line1 in f2:
            if locus_id == line1.split()[1]:
                # num1+=1
                # print (num1)
                # print locus_id
                transcript = line1.split()[2]
                if transcript.startswith('LOC'):
                    # print (locus_id+'\t'+transcript)
                    for i in line1.split():
                        if i.startswith('PF'):
                            print(locus_id + '\t' + transcript + '\t' + i)
                        else:
                            print(locus_id + '\t' + transcript + '\t' + '\t')