Peter Hanson
Peter Hanson

Reputation: 193

Error: must be str, not generator in Python

I am using python and here is a piece of my code:

wp = open(outfile, 'w')
fields = line.split('\t')
gene_ID = fields[0]
chr = fields[1]
strand = fields[2]
start = int(fields[3])
end = int(fields[4])
bc = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N':'N'}       
if strand == '+':
    wp.write(chr_string[start:end]) 
if strand == '-':
    newstart, newend = -(start + 1), -(end + 1)
    wp.write(bc[base.upper()] for base in chr_string[newstart:newend]) <--error pointed at this line

When I try to run my whole code, I get the following message:

TypeError: must be str, not generator

Does anyone know what is wrong with my code that is prompting this?

Upvotes: 2

Views: 7838

Answers (1)

Zaur Nasibov
Zaur Nasibov

Reputation: 22679

bc[base.upper()] for base in chr_string[newstart:newend] is a generator expression. You need to make a string from that via e.g. join method: ''.join(c[base.upper()] for base in chr_string[newstart:newend])

Upvotes: 6

Related Questions