首页 > 解决方案 > 在“while”循环中写入 HTML 文件时 Python“TypeError”

问题描述

TypeError: must be str, not int当下面的这个函数new_file.write在while循环之外时,我得到了错误。

#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 href="' + fetch_link[j] + '>' + fetch_title[j] + '</a></th>' + '\n' +'</tr>')

标签: pythonwhile-loopsyntax-error

解决方案


在 Python 中,无法直接连接字符串和非字符串。您必须使用许多字符串插值语法之一,例如:

news_file.write('<tr>\n<th>{}<\th>\n'.format(i)

推荐阅读