在txt文件中搜索后查找字符串

时间:2013-08-26 19:17:04

标签: python

我想知道sombody是否可以帮助我解决下面的代码:

我有一个名为report.txt的文本文件,其中包含以下内容(所有内容都是同一行):

Printed: 2013-07-12 05:09 PM QC Product: PROT2 CON Level: Level 3 Priority: QC Method RF Result 174 IU/mL Lot Number: 3BQH01 Sample ID: 3BQH01 Instrument ID: DV330681 QC Range 158.0 - 236.0 Comment Completed: 2013-07-12 17:09:14 Comment: Trigger: Manual Trigger Operator C160487AUR Time of Run 2013-07-12 17:09:14 Reagent 13049MA

现在需要检索以下信息(仅限于:)之后的值

QC Product: PROT2 CON
Level: Level 3
Sample ID: 3BQH01

我正在尝试以下代码:

with open ('report.txt', 'r') as inF:
        for line in inF:
            if 'Sample ID:' in line:           
                SID = line.split(':')[1].strip()
            if 'Level:' in line:           
                LEV = line.split(':')[1].strip()                    
            if 'QC Product:' in line:           
                QCP = line.split(':')[1].strip()

有人有想法或其他解决方案吗?

非常感谢您的所有努力和帮助,

请问 柯恩

1 个答案:

答案 0 :(得分:1)

import re

s = ('Printed: 2013-07-12 05:09 PM '
     'QC Product: PROT2 CON '
     'Level: Level 3 '
     'Priority: QC Method RF '
     'Result 174 IU/mL '
     'Lot Number: 3BQH01 '
     'Sample ID: 3BQH01 '
     'Instrument ID: DV330681 '
     'QC Range 158.0 - 236.0 '
     'Comment Completed: 2013-07-12 17:09:14 '
     'Comment: Trigger: Manual Trigger '
     'Operator C160487AUR '
     'Time of Run 2013-07-12 17:09:14 '
     'Reagent 13049MA')

rgx = re.compile('QC Product *: *(.+?)(?<=\S) +'
                 'Level *: *(.+?)(?<=\S) +'
                 'Priority *:.+?'
                 'Sample ID *: *(.+?)(?<=\S) +'
                 'Instrument ID')

print rgx.search(s).groups()

但是这段代码假设这些项目总是在同一个序列中进行分类