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

前端 未结 1 754
[愿得一人]
[愿得一人] 2021-01-07 12:56

So far I have found a way to yield the name, string, and extra string. It works for the second one but does not work for the first one? it\'s so weird because the formats a

相关标签:
1条回答
  • 2021-01-07 13:30

    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.

    0 讨论(0)
提交回复
热议问题