问题
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