Generator not working to split string by particular identifier . Python 2

亡梦爱人 提交于 2019-12-01 01:41:53

The actual problem is, you are not spliting before yielding. So change the code like this

    if line.startswith('@'):
        if name: 
            body, extra = body.split('+',1)
            yield name, body, extra
            body = ''
        name = line
    else:
        body = body + line
body, extra = body.split('+',1)
yield name, body, extra

Also, the following if condition has no effect in the output of the program

if line == '+':
    pass

So, I removed it in the above code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!