jeudi 18 avril 2019

If statement in python return incorrect result

I got a few CSV files. They have 4 headers:

cat name      sample ID    FirstVal      SecondVal

the problem concerns the second column.

Sometimes sample ID is changed to sample Name. I wrote an IF statement to check the header and if in line are both sample ID and sample Name my tempValue should be 0 (it's because in few files I have added empty cols with the name sample ID and I still want use sample Name).

Same when in line is only sample Name, `tempValue should be 0.

Resume: Sample Name(+if empty Sample ID) = tempValue = 0; Sample ID(if not empty, and not Sample Name) = tempValue = 1.

But I do something wrong, coz in file with headers:

cat name      sample ID    FirstVal      SecondVal

after processing my tempValue is 0, when it should be 1 coz if block should just passed and leave tempValue = 1. What I'm done wrong?

My (sample, that's why file_list[i] = file) code:

file_list[i] = 'C:\\Users\\ubunt\\Desktop\\a\\1\\file'
tempValue = 1
def NameOrID():
    Sample = 'sample Name'
    Sample2 = 'sample ID'
    for line in open(file_list[i]):
        if Sample and Sample2 in line:
            print(line)
            tempValue = 0
            return tempValue
            break
        elif Sample in line:
            print(line)
            tempValue = 0
            return tempValue
            break

tempValue = NameOrID()
if(tempValue == 1 ):
    Sample = 'ID'
else:
    Sample = 'Name'
print(tempValue)

Does anyone see what the problem is? P.S. I need a tempValue for later processing.

Aucun commentaire:

Enregistrer un commentaire