Question

The code does something with a line in a text file (that part works - thanks to your help). But if a condition is not met in that line (if it does not contain the word "Intron" or the word "Exon") it should skip to the next line and repeat the action.

dataFile = open('test.txt', 'r')
wordexon = "Exon"
wornintron = "Intron"
for eachLine in dataFile:
    if wordexon or wordintron in Line:
            tmpStr = ' '
            for char in eachLine:
                tmpStr += char
            s = tmpStr.split()
            print '\t'.join((s[0], s[3], s[4]))
    else next(iter)
Was it helpful?

Solution

Just remove the

else next(iter)

That will do.

That said, you could have written your code like this:

for eachLine in dataFile:
    if not ((wordexon in Line) or (wordintron in Line)):
        continue
    tmpStr = ' '
    for char in eachLine:
        tmpStr += char
    s = tmpStr.split()
    print '\t'.join((s[0], s[3], s[4]))

Also, your condition is wrong. It will always enter the if as it is.

OTHER TIPS

  1. Either remove next(iter)

  2. or use continue to make your intent clear.

            s = tmpStr.split()
            print '\t'.join((s[0], s[3], s[4]))
    else:
            continue
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top