运行软件,Python代码出错提示(我的版本是3.6.7): TypeError: write() argument must be str, not bytes 15
Traceback(mostrecentcalllast):File"/usr/lib/python3.6/code.py",line91,inruncodeexec(c...
Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 91, in runcode exec(code, self.locals) File "ab.auto", line 12, in <module> ab = ab + run(solution,c='ab.2') File "/home/nvh/auto/07p/python/AUTOCommands.py", line 1585, in run res = runner.run() File "/home/nvh/auto/07p/python/runAUTO.py", line 377, in run self.runCommand(command, solution) File "/home/nvh/auto/07p/python/runAUTO.py", line 478, in runCommand status = self.__runCommand_noredir(command, solution) File "/home/nvh/auto/07p/python/runAUTO.py", line 534, in __runCommand_noredir self.__write_constants_solution(stdin, solution) File "/home/nvh/auto/07p/python/runAUTO.py", line 515, in __write_constants_solution solution.write(f,mlab=True) File "/home/nvh/auto/07p/python/parseS.py", line 1066, in write write_enc(line+os.linesep) File "/home/nvh/auto/07p/python/parseS.py", line 1032, in write_enc output.write(s.encode("ascii"))TypeError: write() argument must be str, not bytes============================网上有人说是 文件打开有问题,所以去打开了parseS.py文件,下面是Row Line 1032, Row Line 305,Row Line 863&869, Row Line 1169中用到 open()命令的几个地方,求解答!!
展开
1个回答
展开全部
提示write输入的参数是字符串类型str,不是字节类型bytes。
在python3中,不能以任何隐式方式将str和bytes类型二者混合使用。不可以将str和bytes类型进行拼接,不能在str中搜索bytes数据(反之亦然),也不能将str作为参数传入需要bytes类型参数的函数(反之亦然)。
问题应该是出在了s.encode('acsii')上
strings可以被编码(encode)成字bytes,bytes也可以解码(decode)成strings:
>>> '€20'.encode('utf-8')
b'\xe2\x82\xac20'
>>> b'\xe2\x82\xac20'.decode('utf-8')
'€20'
更多追问追答
追问
大哥,我没学过python,这是应用报错,我能打开parseS.py这个文件,修改1032行为"-utf-8"也不行;现在需要知道如何修改,谢谢。
追答
你试试不要了.encode('ascii'),直接变为output.write(s)
或者 改为s.encode('ascii').decode('utf-8')
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询