如何正确读取Python中的双换行符

如何正确读取Python中的双换行符,python,parsing,Python,Parsing,这是我上一个问题的延续,我当时正在努力解析文件。然而,现在我已经将一些代码组合到一个新的解析器中,该解析器可以正确地读取记录。不幸的是,代码在第一条记录之后停止,而我不知道如何区分文件结尾字符和分隔记录的双新闻空间字符。有什么想法吗 此处提供了输入文件: 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. S

这是我上一个问题的延续,我当时正在努力解析文件。然而,现在我已经将一些代码组合到一个新的解析器中,该解析器可以正确地读取记录。不幸的是,代码在第一条记录之后停止,而我不知道如何区分文件结尾字符和分隔记录的双新闻空间字符。有什么想法吗

此处提供了输入文件:

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)

了解如何使用
对infle:
中的行逐行迭代文件。无需使用“”测试文件结尾,for循环迭代将为您完成这项工作:

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

正如@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)

非行
绝对不意味着文件结束。
如果非行:
检查必须在
.strip()
@jasonharper不工作之前进行