0

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 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

0

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)

Comments

0

% 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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.