gpt4 book ai didi

python - 类型错误:write() 参数必须是 str,而不是字节,UTF-16

转载 作者:行者123 更新时间:2023-11-28 22:35:23 24 4
gpt4 key购买 nike

所以我正在使用 Selenium 和 python 运行测试用例,我想为这些测试生成 HTML 测试报告。我找到了应该为我做的这个资源,http://tungwaiyip.info/software/HTMLTestRunner.html如果有人感兴趣,它看起来真的很好,但我一直收到这个错误。

File "facebook.py", line 21, in <module>
HTMLTestRunner.main()
File "C:\Users\kporika\AppData\Local\Programs\Python\Python35-32\lib\unittest\main.py", line 94, in __init__
self.runTests()
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 816, in runTests
unittest.TestProgram.runTests(self)
File "C:\Users\kporika\AppData\Local\Programs\Python\Python35-32\lib\unittest\main.py", line 255, in runTests
self.result = testRunner.run(self.test)
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 631, in run
self.generateReport(test, result)
File "C:\Users\kporika\PycharmProjects\Partha\HTMLTestRunner.py", line 688, in generateReport
self.stream.write(output.encode('UTF-16'))
TypeError: write() argument must be str, not bytes

测试报告生成器的代码在这里https://github.com/tungwaiyip/HTMLTestRunner/blob/master/HTMLTestRunner.py创建者的 github 页面。我该如何解决?

ps 如果有帮助,我正在运行 python 3.5 版。

最佳答案

HTMLTestRunner 是一个 python2 模块。 Python3 区分strbytes 对象,而python2 有unicodestr

正如错误消息所说,第 688 行需要一个 str,而不是 bytes 对象。作为the documentation clarifies , str.encodestr 对象转换为 bytes 对象。您需要将第 688 行修改为 self.stream.write(output),而不是 self.stream.write(output.encode('UTF-16'))

请注意,由于 python2/3 不兼容,很可能会出现更多错误。

关于python - 类型错误:write() 参数必须是 str,而不是字节,UTF-16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com