From 421efdac534b6354982b2cec40392ef2decade85 Mon Sep 17 00:00:00 2001 From: QuiHow Chang Date: Mon, 18 Mar 2024 13:57:18 +0800 Subject: [PATCH] =?UTF-8?q?Add=20`encoding`=20parameter=20for=20non-ASCII?= =?UTF-8?q?=20characters=20Fixed=20the=20`UnicodeDecodeError`=20issue=20in?= =?UTF-8?q?=20`p2j`=20when=20processing=20Python=20scripts=20with=20Chines?= =?UTF-8?q?e=20comments.=20Added=20an=20encoding=20parameter=20(default=3D?= =?UTF-8?q?=E2=80=9Cutf-8=E2=80=9D)=20to=20handle=20character=20encoding.?= =?UTF-8?q?=20Resolves=20Issue=20#16.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- p2j/j2p.py | 7 ++++--- p2j/main.py | 5 +++++ p2j/p2j.py | 13 +++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/p2j/j2p.py b/p2j/j2p.py index ce90f5e..6e8d613 100644 --- a/p2j/j2p.py +++ b/p2j/j2p.py @@ -3,7 +3,7 @@ from p2j.utils import _check_files -def jupyter2python(source_filename: str, target_filename: str, overwrite: bool = False): +def jupyter2python(source_filename: str, target_filename: str, overwrite: bool = False, encoding: str = "utf-8"): """Convert Jupyter notebooks to Python scripts Args: @@ -11,6 +11,7 @@ def jupyter2python(source_filename: str, target_filename: str, overwrite: bool = target_filename (str): Path to name of Python script. Optional. overwrite (bool): Whether to overwrite an existing Python script. with_markdown (bool, optional): Whether to include markdown. Defaults to False. + encoding (str): Encodes obj using the codec registered for encoding. """ target_filename = _check_files( @@ -18,7 +19,7 @@ def jupyter2python(source_filename: str, target_filename: str, overwrite: bool = # Check if source file exists and read try: - with open(source_filename, "r", encoding="utf-8") as infile: + with open(source_filename, "r", encoding=encoding) as infile: myfile = json.load(infile) except FileNotFoundError: print("Source file not found. Specify a valid source file.") @@ -30,6 +31,6 @@ def jupyter2python(source_filename: str, target_filename: str, overwrite: bool = final = "\n\n".join(final) final = final.replace("
", "") - with open(target_filename, "a", encoding="utf-8") as outfile: + with open(target_filename, "a", encoding=encoding) as outfile: outfile.write(final) print("Python script {} written.".format(target_filename)) diff --git a/p2j/main.py b/p2j/main.py index e306758..2eab2c5 100644 --- a/p2j/main.py +++ b/p2j/main.py @@ -19,6 +19,11 @@ def main(): parser.add_argument("-o", "--overwrite", action="store_true", help="Flag whether to overwrite existing target file.") + parser.add_argument("-e", + "--encoding", + default="utf-8", + type=str, + help="Encodes obj using the codec registered for encoding.") args = parser.parse_args() if args.reverse: diff --git a/p2j/p2j.py b/p2j/p2j.py index 2674036..7694271 100644 --- a/p2j/p2j.py +++ b/p2j/p2j.py @@ -17,13 +17,14 @@ TWELVE_SPACES = "{:<12}".format("") -def python2jupyter(source_filename: str, target_filename: str, overwrite: bool = False): +def python2jupyter(source_filename: str, target_filename: str, overwrite: bool = False, encoding: str = "utf-8"): """Convert Python scripts to Jupyter notebooks. Args: source_filename (str): Path to Python script. target_filename (str): Path to name of Jupyter notebook. Optional. overwrite (bool): Whether to overwrite an existing Jupyter notebook. + encoding (str): Encodes obj using the codec registered for encoding. """ target_filename = _check_files( @@ -31,18 +32,18 @@ def python2jupyter(source_filename: str, target_filename: str, overwrite: bool = # Check if source file exists and read try: - with open(source_filename, "r", encoding="utf-8") as infile: + with open(source_filename, "r", encoding=encoding) as infile: data = [l.rstrip("\n") for l in infile] except FileNotFoundError: print("Source file not found. Specify a valid source file.") sys.exit(1) # Read JSON files for .ipynb template - with open(HERE + "/templates/cell_code.json", encoding="utf-8") as file: + with open(HERE + "/templates/cell_code.json", encoding=encoding) as file: CODE = json.load(file) - with open(HERE + "/templates/cell_markdown.json", encoding="utf-8") as file: + with open(HERE + "/templates/cell_markdown.json", encoding=encoding) as file: MARKDOWN = json.load(file) - with open(HERE + "/templates/metadata.json", encoding="utf-8") as file: + with open(HERE + "/templates/metadata.json", encoding=encoding) as file: MISC = json.load(file) # Initialise variables @@ -160,6 +161,6 @@ def python2jupyter(source_filename: str, target_filename: str, overwrite: bool = final.update(MISC) # Write JSON to target file - with open(target_filename, "w", encoding="utf-8") as outfile: + with open(target_filename, "w", encoding=encoding) as outfile: json.dump(final, outfile, indent=1, ensure_ascii=False) print("Notebook {} written.".format(target_filename))