python - 在“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>')
解决方案
在 Python 中,无法直接连接字符串和非字符串。您必须使用许多字符串插值语法之一,例如:
news_file.write('<tr>\n<th>{}<\th>\n'.format(i)
推荐阅读
- css - 匹配css中重复线性渐变模式的开始和结束
- python-3.x - 网络抓取晨星时获取空列表
- swagger - Swagger 错误表明需要在路径或操作级别定义参数
- python - 根据值划分行
- ssh - 无法在 CoreOS 中更改 sshd 的 prot
- android - 为什么 Project 的 build.gradle 没有显示?
- angular - Angular 8,延迟加载父组件和子组件作为默认路由
- url - 更改 TinyMCE 图片上传相对 URL
- javascript - python django - 使用解码的base64写入时图像为空白
- fiddler - 使用 FiddlerScript 或扩展处理 SAZ 文件