到目前为止,我已经找到了一种生成名称、字符串和额外字符串的方法。它适用于第二个但不适用于第一个?这很奇怪,因为格式非常相似。是因为它是多行吗?我以为if line == '+': pass
将绕过该问题。
我在打印中添加了“\n”以显示差异
Input:
@first_name
AlongStringOfText
ThatHasNoSpaces
ButIsSeparatedByLineBreaks
+
{+iuhsfIUHSDFUi8849308989829
0990+-]@@@#*$()(@*$*)))***)@@**@#*u
sdfiuhnknwuiewi
+
@second_name
MoreTextThatCouldBeOnOneLine
+
+{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff
#empty line at end
当前脚本:
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')
期望的输出:
('@first_name','AlongStringOfTextThatHasNoSpacesButIsSeparatedByLineBreaks','{+iuhsfIUHSDFUi88493089898290990+-]@@@#*$()(@*$*)))***)@@**@#*usdfiuhnknwuiewi')
('@second_name','MoreTextThatCouldBeOnOneLine','+{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff')