66
77import os
88import warnings
9+ from contextlib import suppress
910
1011import numpy as np
1112
@@ -266,11 +267,6 @@ def _write_header(fileobj, header):
266267 )
267268 out = '\n ' .join (lines )
268269
269- # Check the header is well formatted.
270- if out .count ('\n ' ) > len (lines ) - 1 : # \n only allowed between lines.
271- msg = f"Key-value pairs cannot contain '\\ n':\n { out } "
272- raise HeaderError (msg )
273-
274270 if out .count (':' ) > len (lines ):
275271 # : only one per line (except the last one which contains END).
276272 msg = f"Key-value pairs cannot contain ':':\n { out } "
@@ -331,6 +327,8 @@ def _read_header(cls, fileobj):
331327 f .seek (1 , os .SEEK_CUR ) # Skip \n
332328
333329 found_end = False
330+ key = None
331+ tmp_hdr = {}
334332
335333 # Read all key-value pairs contained in the header, stop at EOF
336334 for n_line , line in enumerate (f , 1 ):
@@ -343,15 +341,22 @@ def _read_header(cls, fileobj):
343341 found_end = True
344342 break
345343
346- if ':' not in line : # Invalid header line
344+ # Set new key if available, otherwise append to last known key
345+ with suppress (ValueError ):
346+ key , line = line .split (':' , 1 )
347+ key = key .strip ()
348+
349+ # Apparent continuation line before any keys are found
350+ if key is None :
347351 raise HeaderError (f'Invalid header (line { n_line } ): { line } ' )
348352
349- key , value = line .split (':' , 1 )
350- hdr [key .strip ()] = value .strip ()
353+ tmp_hdr .setdefault (key , []).append (line .strip ())
351354
352355 if not found_end :
353356 raise HeaderError ('Missing END in the header.' )
354357
358+ hdr .update ({key : '\n ' .join (val ) for key , val in tmp_hdr .items ()})
359+
355360 offset_data = f .tell ()
356361
357362 # Set the file position where it was, in case it was previously open
0 commit comments