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

南笙酒味 提交于 2019-11-30 21:05:32

问题


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 are really similar. is it because it's multiple lines? i thought the if line == '+': pass would bypass the issue.

i added in the print '\n' to show the difference

Input:

@first_name
AlongStringOfText
ThatHasNoSpaces
ButIsSeparatedByLineBreaks
+
{+iuhsfIUHSDFUi8849308989829
0990+-]@@@#*$()(@*$*)))***)@@**@#*u
sdfiuhnknwuiewi
+
@second_name
MoreTextThatCouldBeOnOneLine
+
+{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff
#empty line at end

Current Script:

def organize(input_file):
    name = None
    body = ''
    extra = ''
    for line in input_file:
        line = line.strip()
        if line.startswith('@'):
            if name: 
                yield name, body, extra
                body = ''
                extra = ''
            name = line
        else:
            body = body + line
            if line == '+':
                pass
    print '\n'
    body,extra = body.split('+',1)

    yield name,body,extra

for line in organize(file_path):
    print line

Output:

('@first_name', 'AlongStringOfTextThatHasNoSpacesButIsSeparatedByLineBreaks+{+iuhsfIUHSDFUi88493089898290990+-]@@@#*$()(@*$*)))***)@@**@#*usdfiuhnknwuiewi+', '')


('@second_name', 'MoreTextThatCouldBeOnOneLine', '+{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff')

Desired Output:

('@first_name','AlongStringOfTextThatHasNoSpacesButIsSeparatedByLineBreaks','{+iuhsfIUHSDFUi88493089898290990+-]@@@#*$()(@*$*)))***)@@**@#*usdfiuhnknwuiewi')
('@second_name','MoreTextThatCouldBeOnOneLine','+{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff')

回答1:


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.



来源:https://stackoverflow.com/questions/26351451/generator-not-working-to-split-string-by-particular-identifier-python-2

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