Python3 执行脚本时遭遇“TypeError: not all arguments converted during string formatting”问题:如何解决?
不知道大家是否熟悉?今天我将给大家介绍《Python3 执行脚本时遭遇“TypeError: not all arguments converted during string formatting”问题:如何解决?》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
python3执行脚本时遭遇“typeerror: not all arguments converted during string formatting”
问题:
在执行python脚本时,遇到了以下错误:
typeerror: not all arguments converted during string formatting
同时,代码中部分涉及的操作为:
cursor.execute(tgt_sql) rows = cursor.fetchall() out_tgt.write('%s\n' % rows)
分析:
错误提示表明,在进行字符串格式化时,并非所有参数都经过转换。这是因为在使用%操作符进行字符串格式化时,对于不同的数据类型,需要使用相应的格式符号。在该情况下,应将rows变量视为元组或数组,需要使用%s格式符号进行转换。
解决方法:
将如下所示:
out_tgt.write('%s\n' % rows)
改为:
out_tgt.write('%s\n' % (rows,))
通过添加括号,将rows变量强制转换为元组,从而解决了格式化问题。
示例代码:
修改后的代码如下:
cursor.execute(tgt_sql) rows = cursor.fetchall() out_tgt.write('%s\n' % (rows,))
修改后,脚本将正常执行。
今天关于《Python3 执行脚本时遭遇“TypeError: not all arguments converted during string formatting”问题:如何解决?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!