【问题标题】:copy section of text in file python复制文件python中的文本部分
【发布时间】:2016-06-15 23:14:32
【问题描述】:

我需要从下面的文本文件中提取值:

fdsjhgjhg
fdshkjhk
Start
Good Morning
Hello World
End
dashjkhjk
dsfjkhk

我需要提取的值是从头到尾。

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    copy = False
    for line in infile:
        if line.strip() == "Start":
            copy = True
        elif line.strip() == "End":
            copy = False
        elif copy:
            outfile.write(line)

我使用的上面的代码来自这个问题: Extract Values between two strings in a text file using python

此代码将不包括字符串“开始”和“结束”,只是其中的内容。您将如何包含外围字符串?

【问题讨论】:

  • 我会使用多行正则表达式 - 代码看起来也更容易

标签: python python-3.x


【解决方案1】:

elifmeans“只有在其他情况失败时才这样做”。它在语法上等同于“else if”、if you're coming from a differnet C-like 语言。没有它,跌倒应该注意包括“开始”和“结束”

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    copy = False
    for line in infile:
        if line.strip() == "Start":
            copy = True
        if copy: # flipped to include end, as Dan H pointed out
            outfile.write(line)
        if line.strip() == "End":
            copy = False

【讨论】:

    【解决方案2】:

    @en_Knight 几乎是对的。这是满足 OP 要求的修复程序,即分隔符包含在输出中:

    with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
        copy = False
        for line in infile:
            if line.strip() == "Start":
                copy = True
            if copy:
                outfile.write(line)
            # move this AFTER the "if copy"
            if line.strip() == "End":
                copy = False
    

    或者在它适用的情况下简单地包含 write():

    with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
        copy = False
        for line in infile:
            if line.strip() == "Start":
                outfile.write(line) # add this
                copy = True
            elif line.strip() == "End":
                outfile.write(line) # add this
                copy = False
            elif copy:
                outfile.write(line)
    

    更新:要回答评论中的问题“仅在 'Start' 之后使用第一次出现的 'End'”,请将最后一个 elif line.strip() == "End" 更改为:

            elif line.strip() == "End" and copy:
                outfile.write(line) # add this
                copy = False
    

    如果只有一个“开始”行但有多个“结束”行,则此方法有效......这听起来很奇怪,但这就是提问者所问的。

    【讨论】:

    • 这很有意义。是否可以选择性地结束副本,仅在“开始”之后使用“结束”的第一次出现。我的文件包含多个字符串“End”?
    • @Dan H 如果End 后面有一个Start 怎么防止复制这个Strat?并立即停止复制
    • @Catalina :选项:1)在看到“结束”后调用 exit()。 2)计算你看到的启动次数;如果这是第一个,则仅将副本设置为“True”。
    【解决方案3】:

    正则表达式方法:

    import re
    
    with open('input.txt') as f:
        data = f.read()
    
    match = re.search(r'\n(Start\n.*?\nEnd)\n', data, re.M | re.S)
    if match:
        with open('output.txt', 'w') as f:
            f.write(match.group(1))
    

    【讨论】:

    • 这可能是更强大的解决方案,但对于不清楚 elif v if 的人,也许您可​​以包含一些文字描述?
    • 这样更好:(^Start[\s\S]+^End)Demo(或者(^Start[\s\S]+?^End),如果有超过1个End...)
    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多