Skip to content

Commit

Permalink
add support for multiple file encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
TOPetit committed Jun 21, 2024
1 parent c2587a7 commit 9ae8c67
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/pygedcom/gedcom_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ def __open(self) -> str:
:return: The content of the GEDCOM file.
:rtype: str
"""
with open(self.path, "r") as file:
data = file.read()
return data
supported_encodings = ["utf-8", "utf-16", "latin1", "ansi"]
for encoding in supported_encodings:
try:
with open(self.path, "r", encoding=encoding) as file:
return file.read()
except UnicodeDecodeError:
continue
raise ValueError(
f"Could not open file {self.path} with supported encodings ({', '.join(supported_encodings)})."
)

def __parse_line(self, line: str) -> dict:
"""Parse a line of a GEDCOM file.
Expand Down

0 comments on commit 9ae8c67

Please sign in to comment.