【问题标题】:Read between certain words and print out whats in between. (Python)在某些单词之间阅读并打印出中间的内容。 (Python)
【发布时间】:2015-08-14 19:36:14
【问题描述】:

您好,我对 python 编程还很陌生,我想知道如何修复我的代码,让我只能在普通文本文档中的两个字符串之间读取。例如,假设我有以下

unimportant data  
unimportant data   
unimportant data 
... ... ...   
First string     #I want to print out starting from this line                     
Important data
Important data
Important data
Important data  
Last String       #Last line I dont want to print this line.
unimportant data  
unimportant data   
unimportant data 
unimportant data  
unimportant data   
unimportant data 

到目前为止,我已经能够制作一个简单的文件 I/O 来读取一行。

data_file = open("test.txt", "r")

for line in data_file:
    if re.match("(.*)First String(.*)", line):
        print(line)

但是这只会打印出第一行。

任何提示或帮助将非常感谢您。

【问题讨论】:

    标签: python string python-2.7 file-io readfile


    【解决方案1】:
    go = False
    start = "First string"
    end = "Last String"
    
    with open('path/to/file') as infile:
        for line in infile:
            line = line.strip()
            if line == start: go = True
            elif line == end:
                go = False
                continue
            if go: print(line)
    

    如果您只是在寻找关键字,而不是匹配整行:

    go = False
    start = "First string"
    end = "Last String"
    
    with open('path/to/file') as infile:
        for line in infile:
            line = line.strip()
            if start in line: go = True
            elif end in line:
                go = False
                continue
            if go: print(line)
    

    【讨论】:

    • 好的,这很好用,但是我对字符串还有另一个问题。与其尝试匹配整个字符串,不如只匹配字符串中的一个关键字?所以假设我有不重要的词不重要的词关键字重要数据重要数据重要数据不重要的词关键字
    • 这很好用!谢谢我会看到随着我的进一步工作,我可以使它变得多么复杂。谢谢!
    【解决方案2】:

    你可以使用itertools.dropwhile:

    from itertools import dropwhile
    def find_section(fle, start, end):
        from itertools import dropwhile
        with open(fle) as f:
            for line in dropwhile(lambda x: not x.startswith(start), f):
                if line.startswith(end):
                    break
                yield line
    
    for line in find_section("in.txt", "First String ", "Last string  "):
        print(line)
    

    输出:

    First string     #I want to print out starting from this line                     
    
    Important data
    
    Important data
    
    Important data
    

    或者结合takewhile和dropwhile:

    from itertools import dropwhile, takewhile
    
    def find_section(fle, start, end):
        with open(fle) as f:
            for line in takewhile(lambda x: not x.startswith(start),
                                  dropwhile(lambda x: not x.startswith(end), f)):
                yield line
    

    或者只使用内部循环:

    def find_section(fle, start, end):
        with open(fle) as f:
            for line in f:
                if line.startswith(start):
                    yield line
                    for _line in f:
                        if _line.startswith(end):
                            return
                        yield _line
    
    
    for line in find_section("in.txt", "First string", "Last String  "):
        print(line)
    

    输出:

    First string     #I want to print out starting from this line                     
    
    Important data
    
    Important data
    
    Important data
    
    Important data  
    

    您想要匹配的任何条件都可以在 lambda 中使用,替换startswith逻辑并使用for循环进行相同的操作

    【讨论】:

      猜你喜欢
      • 2016-11-16
      • 2016-04-06
      • 1970-01-01
      • 2014-06-05
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多