0

我想知道 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()

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

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

亲切的问候科恩

4

1 回答 1

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()

但是这段代码假设项目总是以相同的顺序排列

于 2013-08-26T19:38:07.957 回答