tags:

views:

36

answers:

4

I writing HTML to a text file which is then read by the browser, but I get an error stating "not all arguments converted during string formatting"

But i can't see hwere im going wrong.

z.write('<td><a href=/Plone/query/species_strain?species=%s>'+k+'</td>' % k)
+4  A: 

You're missing parentheses:

z.write(('<td><a href=/Plone/query/species_strain?species=%s>'+k+'</td>') % k)

But it would be better not to mix concatenation and formatting. So consider:

'<td><a href=/Plone/query/species_strain?species=%(k)s>%(k)s</td>' % {'k': k}

You might want to generate HTML using a dedicated tool. Concatenating strings tends to lead to buggy and hard to parse HTML.

Matthew Flaschen
+5  A: 

You're using string concatenation in combination with substitution. Your substitution formatter %s is in the first string, but the % k applies to the last. You should do this:

'<td><a href=/Plone/query/species_strain?species=%s>%s</td>' % (k,k)

Or this:

('<td><a href=/Plone/query/species_strain?species=%s>'+k+'</td>') % k
mipadi
A: 

You get wrong and combining + and string formatting through %. If k contains any %-sequence it would look like this:

'<td...species=%s>...%s...</td>' % k

You get two or more %-sequences and only one argument. You probably want this instead:

'...species=%s>%s</td>' % (k, k)
doublep
A: 

% k must be after string with %s

z.write('<td><a href=/Plone/query/species_strain?species=%s>' % k +k+'</td>')

or better

z.write('<td><a href=/Plone/query/species_strain?species=%s>%s</td>' % (k, k))
jcubic