Count elements in each row

Python code to count the number of elements (genes | proteins | genus ...) in each row in a non-homogenous cluster files.

Example Input:

g1  g2   g3  g4
g2
g4  g6  g7

Example Output:
4
1
3


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
__author__ = 'Arun Prasanna'
'''
Python code to count number of elements in non-homogenous text file. Small, simple & self explanatory code ! 
'''
with open('Input.txt','r') as infile:
    entries = infile.read()
each_line = entries.splitlines()
new_list = []
for row in each_line:
    element = row.split("\t")
    ele_size = len(element)
    new_list.append(str(ele_size))
    new_list.append('\n')

f = open('Count_EachRowElements.txt','w')
out= f.writelines(new_list)
f.close()
print "Program complete"

Comments

Popular posts from this blog

Fasta Header Replacer V2.0

Map multiple annotations using pandas

Condense fasta header