2017-10-13 22 views
-1

これは以前の質問の続きです。hereに投稿しました。ここでは、RISというファイルの解析に苦労していました。しかし、今ではいくつかのコードを新しいパーサに結合しました。これはレコードを正しく読み込みます。残念ながら、コードは最初のレコードの後に​​停止しますが、ファイルの最後と2つの新しいスペース文字を区別する方法はわかりません。何か案が?Pythonで二重改行文字を正しく読み込む方法

Record #1 of 306 
ID: CN-01160769 
AU: Uedo N 
AU: Yao K 
AU: Muto M 
AU: Ishikawa H 
TI: Development of an E-learning system. 
SO: United European Gastroenterology Journal 
YR: 2015 
VL: 3 
NO: 5 SUPPL. 1 
PG: A490 
XR: EMBASE 72267184 
PT: Journal: Conference Abstract 
DOI: 10.1177/2050640615601623 
US: http://onlinelibrary.wiley.com/o/cochrane/clcentral/articles/769/CN-01160769/frame.html 


Record #2 of 306 
ID: CN-01070265 
AU: Krogh LQ 
AU: Bjornshave K 
AU: Vestergaard LD 
AU: Sharma MB 
AU: Rasmussen SE 
AU: Nielsen HV 
AU: Thim T 
AU: Lofgren B 
TI: E-learning in pediatric basic life support: A randomized controlled non-inferiority study. 
SO: Resuscitation 
YR: 2015 
VL: 90 
PG: 7-12 
XR: EMBASE 2015935529 
PT: Journal: Article 
DOI: 10.1016/j.resuscitation.2015.01.030 
US: http://onlinelibrary.wiley.com/o/cochrane/clcentral/articles/265/CN-01070265/frame.html 


Record #3 of 306 
ID: CN-00982835 
AU: Worm BS 
AU: Jensen K 
TI: Does peer learning or higher levels of e-learning improve learning abilities? 
SO: Medical education online 
YR: 2013 
VL: 18 
NO: 1 
PG: 21877 
PM: PUBMED 28166018 
XR: EMBASE 24229729 
PT: Journal Article; Randomized Controlled Trial 
DOI: 10.3402/meo.v18i0.21877 
US: http://onlinelibrary.wiley.com/o/cochrane/clcentral/articles/835/CN-00982835/frame.html 

を、コードは以下の貼り付けられます:

入力ファイルは、ここで提供され

import re 

# Function to process single record 
def read_record(infile): 
    line = infile.readline() 
    line = line.strip() 

    if not line: 
     # End of file 
     return None 

    if not line.startswith("Record"): 
     raise TypeError("Not a proper file: %r" % line) 

    # Read tags and fields 
    tags = [] 
    fields = [] 
    while 1: 
     line = infile.readline().rstrip() 
     if line == "": 
      # Reached the end of the record or end of the file 
      break 
     prog = re.compile("^([A-Z][A-Z0-9][A-Z]?): (.*)") 
     match = prog.match(line) 
     tag = match.groups()[0] 
     field = match.groups()[1] 
     tags.append(tag) 
     fields.append(field) 

    return [tags, fields] 


# Function to loop through records 
def read_records(input_file): 
    records = [] 
    while 1: 
     record = read_record(input_file) 
     if record is None: 
      break 
     records.append(record) 
    return records 


infile = open("test.txt") 

for record in read_records(infile): 
    print(record) 
+1

'not line'はファイルの終わりを絶対に意味しません。 – ForceBru

+0

'if not line:'チェックは '.strip()'の前に行く必要があります。 – jasonharper

+0

@jasonharperは機能しません – Andrej

答えて

1

for line in infile:を使用して行毎にファイルを反復処理する方法を学びます。ループ反復があなたのためにそれを行うだろうために「」でファイルの終わりをテストする必要はあり、:

for line in infile: 
    # remove trailing newlines, and truncate lines that 
    # are all-whitespace down to just '' 
    line = line.rstrip() 

    if line: 
     # there is something on this line 
    else: 
     # this is a blank line - but it is definitely NOT the end-of-file 
0

ここ@PaulMcGによって示唆されるようには行毎にファイルを反復処理ソリューションではありません。

import re 

records = [] 
count_records = 0 
count_newlines = 0 
prog = re.compile("^([A-Z][A-Z0-9][A-Z]?): (.*)") 
bom = re.compile("^\ufeff") 
with open("test.ris") as infile: 
    for line in infile: 
     line = line.rstrip() 
     if bom.match(line): 
      line = re.sub("^\ufeff", "", line) 
     if line: 
      if line.startswith("Record"): 
       print("START NEW RECORD") 
       count_records += 1 
       count_newlines = 0 
       current_record = {} 
       continue 
      match = prog.match(line) 
      tag = match.groups()[0] 
      field = match.groups()[1] 
      if tag == "AU": 
       if tag in current_record: 
        current_record[tag].append(field) 
       else: 
        current_record[tag] = [field] 
      else: 
       current_record.update({tag: field}) 
     else: 
      count_newlines += 1 
      if count_newlines > 1 and count_records > 0: 
       print("# of records: ", count_records) 
       print("# of newlines: ", count_newlines) 
       records.append(current_record) 
関連する問題