Python3输出到html写文件需注意,出现前缀b'

来源:互联网 发布:实时网速监控软件 编辑:程序博客网 时间:2024/05/22 06:19

    def output_html(self):        fout=open('output.html','w',encoding='UTF-8', newline='')        fout.write('<html>')        fout.write('<head>')        fout.write('<meta charset = "UTF-8"/>')        fout.write('</head>')        fout.write('<body>')        fout.write('<table>')        for data in self.datas:            fout.write('<tr>')            fout.write('<td>%s</td>' % data['url'])            fout.write('<td>%s</td>' % data['title'])            fout.write('<td>%s</td>' % data['summary'])            fout.write('</tr>')        fout.write('</table>')        fout.write('</body>')        fout.write("</html>")

要注意open方法,还有指定网页的编码格式为utf-8,否则会出现字符串b前缀和编码错误


UnicodeEncodeError: 'gbk' codec can't encode character '\u02c8'


0 0
原创粉丝点击