简体   繁体   English

更好的调试:期望一个字符缓冲区对象

[英]Better debugging: expected a character buffer object

I am trying to pass a string section to the the python function below it I am uncertain why I am seeing this error. 我正在尝试将字符串部分传递给它下面的python函数,我不确定为什么会看到此错误。 My understanding of this is that it is not getting a string, where it is expected. 我对此的理解是,它没有在预期的位置得到字符串。 I have tried casting, but that is not working either. 我已经尝试了投射,但是那也不起作用。 How can I solve this or get more debug info? 如何解决此问题或获取更多调试信息?

section = str('[log]')
some_var = 'filename ='

edit_ini('./bench_config.ini', section, some_var, 'logs/ops_log_1')

The function causing the error 导致错误的函数

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()

However I see the error: 但是我看到错误:

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

You overwrite the passed-in section with section=False . 您用section=False覆盖了传入的section The error is because you cannot call string.startswith( False ) . 该错误是因为您无法调用string.startswith( False )

A way to debug python is to use pdb . 调试python的一种方法是使用pdb This would have helped you find your problem here. 这样可以帮助您在此处找到问题。 You should read the spec, but heres a quick guide on how to use pdb. 您应该阅读规范,但是这里是有关如何使用pdb的快速指南。

import pdb
# your code ...
pdb.set_trace() # put this right before line.startswith(section)

Then when you run your code, you will execute up to right before the failure. 然后,当您运行代码时,将在故障发生之前立即执行。 Then you can print section in pdb, and see that it is False , and then try to figure out why it is False . 然后,您可以在pdb中打印该section ,并查看它是否为False ,然后尝试找出它为什么为False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM