[英]Better debugging: expected a character buffer object
我正在尝试将字符串部分传递给它下面的python函数,我不确定为什么会看到此错误。 我对此的理解是,它没有在预期的位置得到字符串。 我已经尝试了投射,但是那也不起作用。 如何解决此问题或获取更多调试信息?
section = str('[log]')
some_var = 'filename ='
edit_ini('./bench_config.ini', section, some_var, 'logs/ops_log_1')
导致错误的函数
def edit_ini(filename, section, some_var, value):
section = False
flist = open(filename, 'r').readlines()
f = open(filename+'test', 'w')
for line in flist:
line = str(line)
print line
if line.startswith(section):
section = True
if( section == True ):
if( line.startswith(some_var) ):
modified = "%s = $s", variable, value
print >> f, modified
section = False
else:
print >> f, line
f.close()
但是我看到错误:
Traceback (most recent call last):
File "bench.py", line 89, in <module>
edit_ini('./config.ini', section, some_var, 'logs/log_1')
File "bench.py", line 68, in edit_ini
if line.startswith(section):
TypeError: expected a character buffer object
您用section=False
覆盖了传入的section
。 该错误是因为您无法调用string.startswith( False )
。
调试python的一种方法是使用pdb 。 这样可以帮助您在此处找到问题。 您应该阅读规范,但是这里是有关如何使用pdb的快速指南。
import pdb
# your code ...
pdb.set_trace() # put this right before line.startswith(section)
然后,当您运行代码时,将在故障发生之前立即执行。 然后,您可以在pdb中打印该section
,并查看它是否为False
,然后尝试找出它为什么为False
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.