2

I have a tsv file and I'd like to iterate through all the rows. this is my code:

import csv

with open('tsv2.tsv','r') as tsvin:
    tsvin = csv.reader(tsvin, delimiter='\t')
    tsv_file = tsvin

    def non_synonymous_filter(tsv_file):
        non_synonymous_list=[]
        for row in range(len(tsv_file)):
            if "NON_SYNONYMOUS_CODING" in row[index]:
                non_synonymous_list.append(row)

        return non_synonymous_list

    print(non_synonymous_filter(tsv_file))

The problem is I get this error message: object of type '_csv.reader' has no len()

Bella
  • 937
  • 1
  • 13
  • 25

1 Answers1

0

tsvin/tsv_file in your code is a CSV reader, not a list or an array containing your text. I suggest reading lines like this and then reviewing your inner method:

with open('tsv2.tsv','r') as tsvin:
    for line in csv.reader(tsvin, delimiter='\t'):
        print line

I am not clear on what you exactly want (for example what is "index"?), but maybe this:

non_synonymous_list=[]
with open('tsv2.tsv','r') as tsvin:
    non_synonymous_list = [line for line in csv.reader(tsvin, delimiter='\t') if "NON_SYNONYMOUS_CODING" in line]

which returns something like:

[['NON_SYNONYMOUS_CODING', '2nd col', '3rd col']

aless80
  • 3,122
  • 3
  • 34
  • 53