import os import sys sys.path.append(os.path.dirname(__file__) + "/../") import logging import traceback import chardet from format_convert import get_memory_info @get_memory_info.memory_decorator def txt2text(path): logging.info("into txt2text") try: # 判断字符编码 with open(path, "rb") as ff: data = ff.read() encode = chardet.detect(data).get("encoding") print("txt2text judge code is", encode) try: if encode is None: logging.info("txt2text cannot judge file code!") return [-3] with open(path, "r", encoding=encode) as ff: txt_text = ff.read() return [txt_text] except: logging.info("txt2text cannot open file with code " + encode) return [-3] except Exception as e: print("txt2text", traceback.print_exc()) logging.info("txt2text error!") return [-1]