From e7640566606d0a3a9d9b420098b8ebd54d13a31d Mon Sep 17 00:00:00 2001 From: Ciges Date: Wed, 19 Feb 2025 21:10:19 +0100 Subject: [PATCH] - Modifications on how to open Obsidian notes to avoid UnicodeDecodeError - Regular expressions are searched an replaced with MULTILINE flag on (^ matches the beginning of each line) --- pyomd/note.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyomd/note.py b/pyomd/note.py index be0d37d..b647af9 100644 --- a/pyomd/note.py +++ b/pyomd/note.py @@ -30,7 +30,7 @@ def __init__(self, path: Union[Path, str]): """ self.path: Path = Path(path) try: - with open(self.path, "r") as f: + with open(self.path, "r", encoding="utf-8", newline='\n') as f: self.content: str = f.read() except Exception as e: raise NoteCreationError(path=path, exception=e) from e @@ -73,7 +73,7 @@ def sub(self, pattern: str, replace: str, is_regex: bool = False): """ if not is_regex: pattern = re.escape(pattern) - self.content = re.sub(pattern, replace, self.content) + self.content = re.sub(pattern, replace, self.content, flags=re.MULTILINE) def update_content( self, @@ -130,7 +130,7 @@ def write(self, path: Union[Path, None] = None): path to the note. If None, overwrites the current note content. """ p = self.path if path is None else path - with open(p, "w") as f: + with open(p, "w", encoding="utf-8", newline='\n') as f: f.write(self.content) @staticmethod