2017-08-03 5 views
-1

コードに記載されているこのデータ文字列ファイルからフィールドを抽出するために、次のコードを使用しています。合計Amt。$ 179720しかし、数字でのみ停止しないでください...ファイルの残りの部分を合計Amtから最後まで抽出します。含まれている要約... から抽出できますか?テキストファイルの正規表現

現在は合計AMTの '[...

を下の行を抽出するが、番号が抽出された取得することはできません。 :$ 179720(推定)\ nInvestigator:スティーブン・R. Palumbi(主任研究者 ']

############################################################ 
pat_Amt=re.compile('Total Amt.*Investigator',re.M|re.DOTALL) 
#print(pat_Amt) 
Amt_term=pat_Amt.findall(data) 
print(Amt_term) 


Amt=int(filter(str.isdigit, Amt_term)) 
### Converting list to string 
Amt=''.join(Amt) 
############################################################ 

あなたが出てくることができている場合は、それぞれの行をループしていない完全なコード

import re 
import pprint 
import os 

from collections import defaultdict 
from pprint  import pprint 


data = """Title  : CRB: Genetic Diversity of Endangered Populations of Mysticete Whales: 
       Mitochondrial DNA and Historical Demography 
Type  : Award 
NSF Org  : DEB 
Latest 
Amendment 
Date  : August 1, 1991  
File  : a9000006 
Award Number: 9000006 
Award Instr.: Continuing grant        
Prgm Manager: Scott Collins       
      DEB DIVISION OF ENVIRONMENTAL BIOLOGY  
      BIO DIRECT FOR BIOLOGICAL SCIENCES   
Start Date : June 1, 1990  
Expires  : November 30, 1992 (Estimated) 
Expected 
Total Amt. : $179720    (Estimated) 
Investigator: Stephen R. Palumbi (Principal Investigator current) 
Sponsor  : U of Hawaii Manoa 
      2530 Dole Street 
      Honolulu, HI 968222225 808/956-7800 

NSF Program : 1127  SYSTEMATIC & POPULATION BIOLO 
Fld Applictn: 0000099 Other Applications NEC     
       61  Life Science Biological     
Program Ref : 9285, 
Abstract : 

       Commercial exploitation over the past two hundred years drove     
       the great Mysticete whales to near extinction. Variation in     
       the sizes of populations prior to exploitation, minimal       
       population size during exploitation and current population      
       sizes permit analyses of the effects of differing levels of      
       exploitation on species with different biogeographical       
       distributions and life-history characteristics. Dr. Stephen     
       Palumbi at the University of Hawaii will study the genetic      
       population structure of three whale species in this context,     
       the Humpback Whale, the Gray Whale and the Bowhead Whale. The     
       effect of demographic history will be determined by comparing     
       the genetic structure of the three species. Additional studies     
       will be carried out on the Humpback Whale. The humpback has a     
       world-wide distribution, but the Atlantic and Pacific       
       populations of the northern hemisphere appear to be discrete     
       populations, as is the population of the southern hemispheric     
       oceans. Each of these oceanic populations may be further      
       subdivided into smaller isolates, each with its own migratory     
       pattern and somewhat distinct gene pool. This study will      
       provide information on the level of genetic isolation among      
       populations and the levels of gene flow and genealogical      
       relationships among populations. This detailed genetic       
       information will facilitate international policy decisions      
       regarding the conservation and management of these magnificent     
       mammals.""" 

year_list=[] 
Total_Amt_list=[] 
abstract_list=[] 


for line in data: 
    #print(line) 

    #pat_file=re.compile('File.*',re.M|re.DOTALL) 
    #file=pat_file.findall(data) 
    #file=''.join(file) 
    #print(file) 
    #print(type(file)) 




    pat_abstract=re.compile('Abstract.*',re.M|re.DOTALL) 
    abstract=pat_abstract.findall(data) 
    abstract=''.join(abstract) 
    #print(abstract) 
    #print(type(abstract)) 


    pat_year=re.compile('Start Date.*Expires',re.M|re.DOTALL) 
    year_term=pat_year.findall(data) 

    ### Converting list to string 
    year_term=''.join(year_term) 

    ### Finding the start year. The result of the findall is a list 
    year=re.findall('[1-2][0-9][0-9][0-9]',year_term) 

    ###converting list to integer 
    for item in year: 
     year=int(item) 

    #print(type(year)) 
    #print(year) 

    # Creating lists for filename, year and abstract  
    # filename is saved for reference 
    #file_list.append(file) 
    #print(file_list) 
    year_list.append(year) 
    #print(year) 
    abstract_list.append(abstract) 
    #print(abstract_list) 
+0

「再」を使用することは過剰です。 'split()'を試してください – nutmeg64

+0

パフォーマンスを探してくれてありがとう、ありがとう、パンダに抽出してロードするだけです... –

+0

パフォーマンスについて話しません。正規表現は強力なツールです。従来の形式を持たないテキストを扱うなど、入力がどの形式であるかわからない場合にのみ使用してください。あなたの場合、私は同じフィールドから同じ値を抽出したいと思うようです( '$'の後の数字だけが異なっています)。したがって、パフォーマンスを考慮しないで 'split()'を使用してください。それはあなたの人生を楽にします。 – nutmeg64

答えて

2

。あなたが望む項目のそれぞれについて、正しい正規表現パターンと

例えばラインから値179720123を取得する:

Total Amt. : $17972(Estimated) 

あなたはこれを行うことができます。

# first look for matching pattern (Total Amt. : $xxx) where xxx are some numbers 
total_amt_line=re.findall('Total Amt\.\s+: \$[0-9]*', data) 

print(total_amt_line) 
>>> ['Total Amt. : $179720'] 

# if there are any matches found extract any number values out of it 
if len(total_amt_line) > 0: 
    total_amt = re.findall('[0-9]+', total_amt_line[0]) 
    print(total_amt) 
    >>> ['179720'] 
1

あなたが探しているパターンがある:r"\$(\d+(?:\.\d+)?)"