我有以下格式的文本文件:
AAAAATTTTTT
AAATTTTTTGGG
TTTDDDCCVVVVV
我正在尝试计算在行首和行尾按顺序出现的字符数。
我写了以下函数:
def getStartEnd(sequence):
start = sequence[0]
end = sequence[-1]
startCount = 0
endCount = 0
for char in sequence:
if char == start:
startCount += 1
if ( char != start):
break
for char in reversed(sequence):
if char == end:
endCount += 1
if ( char != end):
break
return startCount, endCount
此函数在字符串上独立工作。例如:
seq = "TTTDDDCCVVVVV"
a,b = getStartEnd(seq)
print a,b
但是当我插入一个 for 循环时,它只在文件的最后一行给出正确的值。
file = open("Test.txt", 'r')
for line in file:
a,b = getStartEnd(str(line))
print a, b