生成器无法按特定标识符拆分字符串.的Python 2 [英] Generator not working to split string by particular identifier . Python 2

查看:79
本文介绍了生成器无法按特定标识符拆分字符串.的Python 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经找到一种产生名称,字符串和额外字符串的方法.它适用于第二个,但不适用于第一个吗?太奇怪了,因为格式真的很相似.是因为它是多行吗?我以为if line == '+': pass会绕过该问题.

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.

我在印刷品'\ n'中添加了文字,以显示差异

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

输入:

@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

输出:

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


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

所需的输出:

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

推荐答案

实际的问题是,在屈服之前您不是split.所以像这样更改代码

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

此外,以下if条件对程序的输出没有影响

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.

这篇关于生成器无法按特定标识符拆分字符串.的Python 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆