0

我是在 python 中使用正则表达式的新手。我无法弄清楚如何执行以下操作:

我有一堆文本描述作为字符串,如下所示:

FX0XST001ALF89  OLIGO: Bacillus_cand1=ATGCGGTTCAAAATGTTATC      
FILE:/home/AAFC-AAC/fungs/biodiversity/pipelines/454PipelineOutput/v7_newest_testrun_full/rs75/plate1/FX0XST001.MID13/FX0XST001.MID13.sff.trim.fasta    
Project: SAGES  SFF: FX0XST001  SFF.MID: FX0XST001.MID13    
Plate: 1.1     MID_all: MID13   MID: 13 Sample: BK104   
Collector: BK   Year: 2008  Week:   Year_Week:  
Location: Ottawa_ON     City: Ottawa    Province: ON    Crop:   
Treatment:    Substrate_all: Air    Substrate: Air  Target: Bacteria    
Forward Primer: Bac16S27F   Reverse Primer: Bac16S690R  Taq: T

我希望能够提取这个大字符串中的类别并将它们存储到数据库或其他东西中,例如:

Year: 2008
Sample: BK104
Collector: BK

etc...

如何在 python 中使用正则表达式来实现这一点?

我正在考虑使用搜索:

match = re.search(r'Sample:\w\w\w\w\w', theTextDescription)

问题是每个“字段”中的文本长度不同。我真的不知道如何考虑到这一点

4

2 回答 2

2

something like this, you can use \w+ to match characters to any number of length:

In [37]: strs
Out[37]: 'FX0XST001ALF89  OLIGO: Bacillus_cand1=ATGCGGTTCAAAATGTTATC      \nFILE:/home/AAFC-AAC/fungs/biodiversity/pipelines/454PipelineOutput/v7_newest_testrun_full/rs75/plate1/FX0XST001.MID13/FX0XST001.MID13.sff.trim.fasta    \nProject: SAGES  SFF: FX0XST001  SFF.MID: FX0XST001.MID13    \nPlate: 1.1     MID_all: MID13   MID: 13 Sample: BK104   \nCollector: BK   Year: 2008  Week:   Year_Week:  \nLocation: Ottawa_ON     City: Ottawa    Province: ON    Crop:   \nTreatment:    Substrate_all: Air    Substrate: Air  Target: Bacteria    \nForward Primer: Bac16S27F   Reverse Primer: Bac16S690R  Taq: T'

In [38]: re.findall(r"\w+:\s\w+",strs)
Out[38]: 
['OLIGO: Bacillus_cand1',
 'Project: SAGES',
 'SFF: FX0XST001',
 'MID: FX0XST001',
 'Plate: 1',
 'MID_all: MID13',
 'MID: 13',
 'Sample: BK104',
 'Collector: BK',
 'Year: 2008',
 'Location: Ottawa_ON',
 'City: Ottawa',
 'Province: ON',
 'Substrate_all: Air',
 'Substrate: Air',
 'Target: Bacteria',
 'Primer: Bac16S27F',
 'Primer: Bac16S690R',
 'Taq: T']

or may be store it in a dictionary:

In [39]: dict(x.split(":") for x in  re.findall(r"\w+:\s\w+",strs))
Out[39]: 
{'City': ' Ottawa',
 'Collector': ' BK',
 'Location': ' Ottawa_ON',
 'MID': ' 13',
 'MID_all': ' MID13',
 'OLIGO': ' Bacillus_cand1',
 'Plate': ' 1',
 'Primer': ' Bac16S690R',
 'Project': ' SAGES',
 'Province': ' ON',
 'SFF': ' FX0XST001',
 'Sample': ' BK104',
 'Substrate': ' Air',
 'Substrate_all': ' Air',
 'Taq': ' T',
 'Target': ' Bacteria',
 'Year': ' 2008'}
于 2012-11-20T18:43:00.357 回答
1

Make use of the quantifiers of the regex language:

? = 0 or 1

* = 0 or more

+ = 1 or more

match = re.search(r'Sample:\s\w+', theTextDescription)
于 2012-11-20T18:42:55.603 回答