From 900d8a37b58b6302f5181825963f54366aced6ec Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Mon, 31 Aug 2020 19:03:52 +0200 Subject: [PATCH] Simplify read_h5meta --- pyzebra/h5.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pyzebra/h5.py b/pyzebra/h5.py index 2a50db8..a206bf3 100644 --- a/pyzebra/h5.py +++ b/pyzebra/h5.py @@ -11,20 +11,19 @@ def read_h5meta(filepath): dict: A dictionary with section names and their content. """ h5meta_content = dict() - with open(filepath, "r") as h5meta_file: - line = h5meta_file.readline() - while line: - if line.startswith("#begin"): - # read section - section = line[7:-1] # len("#begin ") = 7 + with open(filepath) as h5meta_file: + section = None + for line in h5meta_file: + line = line.strip() + if line.startswith("#begin "): + section = line[len("#begin "):] h5meta_content[section] = [] - line = h5meta_file.readline() - while not line.startswith("#end"): - h5meta_content[section].append(line[:-1]) - line = h5meta_file.readline() - # read next line after section's end - line = h5meta_file.readline() + elif line.startswith("#end"): + section = None + + elif section: + h5meta_content[section].append(line) return h5meta_content