Ad

Python "TypeError" When Writing To HTML File In "while" Loop

- 1 answer

I have getting error for TypeError: must be str, not int This function below new_file.write works fine when it outside the while loop.

#write Data
i = 0
j = 0
while i < 10:
    i = i+1
    j = j+1
    news_file.write('<tr>\n' + '<th>' + i + '<\th>' + '\n')
    news_file.write('<th id="title"><a target="_blank" rel="nofollow noreferrer" href="' + fetch_link[j] + '>' + fetch_title[j] + '</a></th>' + '\n' +'</tr>')
Ad

Answer

In Python it is not possible to directly concat a string and non-strings. You'll have to use one of the many string interpolation syntaxes, for example:

news_file.write('<tr>\n<th>{}<\th>\n'.format(i)
Ad
source: stackoverflow.com
Ad