From 1b598903ad0b29c3ce30d3663fb9290ecb65d170 Mon Sep 17 00:00:00 2001 From: Mattijs Borst <41747007+MattBrst@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:46:07 +0200 Subject: [PATCH] Add secondary encoding (cp1252) to GEF reader If the GEF file cannot be read with the default encoding (UTF8) it will fall back onto the cp1252 encoding. This helps to accept GEF files as commonly produced by dutch suppliers. For more description of the problem, see: https://github.com/Deltares/GEOLib-Plus/issues/6#issue-1329605905 --- imodqgis/gef/reading.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/imodqgis/gef/reading.py b/imodqgis/gef/reading.py index 3d4a11a..572a335 100644 --- a/imodqgis/gef/reading.py +++ b/imodqgis/gef/reading.py @@ -206,11 +206,16 @@ def __repr__(self): return f"{self.__class__.__name__}(nr={self.nr})" def __open_file(self, path): - with open(path, "r") as f: - text = f.read() - end_header = re.search(r"(?P#EOH[=\s+]+)", text).group("eoh") - - self._header, self._data = text.split(end_header) + try: + with open(path, "r") as f: + text = f.read() + except UnicodeDecodeError: + with open(path, "r", encoding='cp1252') as f: + text = f.read() + + end_header = re.search(r"(?P#EOH[=\s+]+)", text).group("eoh") + + self._header, self._data = text.split(end_header) self.parse_header() self.parse_data()