From 8fa8b4b141249828c572c462289769d257ba82c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Tue, 13 Aug 2024 17:35:49 +0800 Subject: [PATCH] Try common encoding formats when reading a file. --- metagpt/tools/libs/editor.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/metagpt/tools/libs/editor.py b/metagpt/tools/libs/editor.py index c2fdcb859..eed92e96b 100644 --- a/metagpt/tools/libs/editor.py +++ b/metagpt/tools/libs/editor.py @@ -219,9 +219,25 @@ class Editor(BaseModel): @staticmethod def _read_text(path: Union[str, Path]) -> List[str]: - with open(str(path), "r") as f: - lines = f.readlines() - return lines + encoding_format_list = [ + "utf-8", + "ascii", + "gb2312", + "gbk", + "iso-8859-1", + "cp1252", + "utf-16", + "utf-16-le", + "utf-16-be", + ] + for encoding in encoding_format_list: + try: + with open(str(path), "r", encoding=encoding) as f: + lines = f.readlines() + return lines + except: + pass + return [f"Reading failed: `{path}` cannot be decoded by `{encoding_format_list}`. Please ask a human for help."] @staticmethod async def _read_pdf(path: Union[str, Path]) -> List[str]: