Repository: biopython/biopython

Issue:
KeyError when using Medline.parse
### Setup

I am reporting a problem with Biopython version, Python version, and operating
system as follows:

3.10.6 (main, Oct  7 2022, 20:19:58) [GCC 11.2.0]
CPython
Linux-4.14.330-250.540.amzn2.x86_64-x86_64-with-glibc2.31
1.82

### Expected behaviour

This should work and parse records as expected:
`records = Medline.parse(lines)`

### Actual behaviour
```
File /opt/conda/lib/python3.10/site-packages/Bio/Medline/__init__.py:175, in parse(handle)
    173         record[key][-1] += line[5:]  # including space using line[5:]
    174     else:
--> 175         record[key].append(line[6:])
    176 elif line:
    177     key = line[:4].rstrip()

KeyError: 'AB'
```

### Steps to reproduce

Offending record:
```
PMID- 29939664
STAT- Publisher
CTDT- 20220621
PB  - StatPearls Publishing
DP  - 2023 Jan
TI  - Morvan Syndrome.
BTI - StatPearls
AB  - Morvan syndrome or Morvan's fibrillary chorea (MFC) is a rare constellation of 
      neurological symptoms, consisting of peripheral nerve hyperexcitability, 
      autonomic instability, and encephalopathy often associated with autoantibodies to 
      voltage-gated potassium channel complexes (VGKCs). On 12 April 1890, the French 
      physician, Dr. Augustine Marie Morvan first published a novel description of this 
      neurological syndrome in La Gazette Hebdomadaire de Medecine et de Chirurgie.  He 
      called it "la choree fibrillaire," which we now know as Morvan syndrome. There 
      have been approximately 60 cases published in the French and other literature but 
      only a few in English literature since then. See table 1 in media section at the 
      end of this script for a review and comparison of the varied clinical features 
      and clinical outcomes in 20 such cases reported in English literature.            
                                                                                        
                                            As per table 1, Morvan syndrome is 
      predominantly a male-dominant entity with a male to female ratio of 19 to 1. The 
      only female case was a rheumatoid arthritis patient on gold therapy who developed 
      mild Morvan syndrome features, and whose condition reverted once treatment was 
      discontinued. Insomnia, hyperhidrosis, dysautonomia, and myokymia were consistent 
      findings noted in 100% (all 20) of the patients. Whereas, hallucinations were 
      seen in 75% (15) of the patients and anti-voltage-gated potassium channel 
      antibodies (VGKC) were observed in 45% (9) of the patients. Another very 
      consistent finding was the conspicuous absence of seizures in 100% (all 20) of 
      the cases and benign findings on MRI in 100% (all 20) in contrast to Limbic 
      encephalitis where seizures and temporal lobe structural abnormalities on MRI are 
      classic findings. Myokymia was seen in 100% (all 20) of the patients, and it was 
      confirmed in most 80%(16) cases with EMG studies which showed spontaneous, either 
      repetitive or continuous muscle activity in the form of fasciculations which were 
      a combination doublet, triplet, multiplet, or neuromyotonic discharges. Other 
      sporadic findings were elevated manganese levels in 5% (1), oligoclonal bands in 
      cerebrospinal fluid (CSF) in 15% (3), thymoma in 40% (8), Acetylcholine receptor 
      (AchR) antibodies in 30% (6) of patients. However, AchR antibodies in association 
      with myasthenic features were seen in only 10% (2) of the patients. Twenty 
      percent (4) of the patients had AchR antibodies without myasthenic features. 
      Treatment modalities tried with varied effectiveness were thymectomy in 25% (5), 
      anticonvulsant therapy in 45% (9), immunosuppression in 50% (10), and IVIG in 20% 
      (4) of the patients. The most effective treatment was plasma exchange which was 
      tried in 55% (11) of the patients, all of whom except one patient showed dramatic 
      improvement. Treatment effectiveness was even more significant when 
      immunosuppression and plasma exchange was tried together. Death was the outcome 
      in only 20% (4) of the patients.
CI  - Copyright (c) 2023, StatPearls Publishing LLC.
FAU - Masood, Wajeed
AU  - Masood W
AD  - Methodist University, Campbell University School of Osteopathic Medicine.
FAU - Sitammagari, Kranthi K
AU  - Sitammagari KK
AD  - Methodist Un, Campbell Un School of OM
LA  - eng
PT  - Study Guide
PT  - Book Chapter
PL  - Treasure Island (FL)
COIS- Disclosure: Wajeed Masood declares no relevant financial relationships with 
      ineligible companies. Disclosure: Kranthi Sitammagari declares no relevant 
      financial relationships with ineligible companies.
EDAT- 2022/06/21 00:00
CRDT- 2022/06/21 00:00
AID - NBK507887 [bookaccession]
```

I believe it might be failing due to the extra blank line in the abstract.


Discussion:
@bgoerdt-ch  Would you be able to suggest how to correct the code?

Resolving patch:
diff --git a/Bio/Medline/__init__.py b/Bio/Medline/__init__.py
--- a/Bio/Medline/__init__.py
+++ b/Bio/Medline/__init__.py
@@ -166,19 +166,26 @@ def parse(handle):
     key = ""
     record = Record()
     for line in handle:
-        line = line.rstrip()
         if line[:6] == "      ":  # continuation line
+            line = line.rstrip()
+            if line == "":
+                # All blank continuation lines should be considered a new line.
+                # See issue #4557
+                line = "      \n"
+
             if key in ["MH", "AD"]:
                 # Multi-line MESH term, want to append to last entry in list
                 record[key][-1] += line[5:]  # including space using line[5:]
             else:
                 record[key].append(line[6:])
-        elif line:
+        elif line != "\n" and line != "\r\n":
+            line = line.rstrip()
             key = line[:4].rstrip()
             if key not in record:
                 record[key] = []
             record[key].append(line[6:])
         elif record:
+            # End of the record
             # Join each list of strings into one string.
             for key in record:
                 if key in textkeys:


Test patch: