-3

我的错误

  File "controller.py", line 26
    try:
      ^
IndentationError: expected an indented block

我的代码:

def main():
    config=ConfigParser.ConfigParser()
    config.readfp(open("settings.cfg"),"r")
    for site in config.sections():
       # ipdb.set_trace()
        settings=dict(config.items(site))
        with open('shoes.txt') as fp: <--new code trying to add
            for category, url in csv.reader(fp):  <--new code trying to add
            #ipdb.set_trace()
            #print url,category
            try: <--line 26

出于某种原因,我收到错误,我不确定如何解决它你能帮我吗?

我有一个旧的,效果很好:

def main():
    config=ConfigParser.ConfigParser()
    config.readfp(open("settings.cfg"),"r")
    for site in config.sections():
       # ipdb.set_trace()
        settings=dict(config.items(site))
        for (url,category) in zip(settings['url'].split(","),settings['category'].split(",")):
            #ipdb.set_trace()
            #print url,category
            try:
... more code
4

2 回答 2

2

我认为尝试不应该是 for 循环的一部分

def main():
    config=ConfigParser.ConfigParser()
    config.readfp(open("settings.cfg"),"r")
    for site in config.sections():
       # ipdb.set_trace()
        settings=dict(config.items(site))
        with open('shoes.txt') as fp: <--new code trying to add
            for category, url in csv.reader(fp):  <--new code trying to add
                pass
            #ipdb.set_trace()
            #print url,category
            try: <--line 26

python 中不可能有空块,“for”语句有一个空块。只需添加一个“通行证”(在这种情况下什么都不做)。

其他变体,似乎与预期的行为相匹配:

def main():
    config=ConfigParser.ConfigParser()
    config.readfp(open("settings.cfg"),"r")
    for site in config.sections():
       # ipdb.set_trace()
        settings=dict(config.items(site))
        with open('shoes.txt') as fp: <--new code trying to add
            for category, url in csv.reader(fp):  <--new code trying to add
                #ipdb.set_trace()
                #print url,category
                try: <--line 26
于 2013-10-19T16:30:02.850 回答
1

编辑:

现在我重写了我的帖子,因为我知道更多关于你的问题是什么......

您的旧代码工作的原因是它在 for 循环中有一些东西,即 try/except 块。然而,新代码没有(评论不计入在内)。

要解决您的问题,请确保您的缩进良好并将某些内容放入 for 循环中。您的代码可能应该如下所示:

def main():
    config=ConfigParser.ConfigParser()
    config.readfp(open("settings.cfg"),"r")
    for site in config.sections():
        # ipdb.set_trace()
        settings=dict(config.items(site))
        with open('shoes.txt') as fp: <--new code trying to add
            for category, url in csv.reader(fp):  <--new code trying to add
                #ipdb.set_trace()
                #print url,category
                try:
                    ...
于 2013-10-19T16:26:51.903 回答