0

我正在使用pylanguagetool ,它是LanguageTool的 Python 包装器,一个拼写/语法校正应用程序。它在 Python 和 Windows 命令行中运行。它在命令行中特别有用,因为它以下列方式显示输出:

在此处输入图像描述

我想捕获此输出,最好采用相同的格式。但是,捕获命令行输出的标准方法似乎会产生编码错误。例如,

pylanguagetool Text_01.txt > Text_01_comments.txt

生成以下错误:

    C:\Users\danie\Desktop\Python\Grading\HI333\Test>pylanguagetool Text_01.txt > Text_01_comments.txt
Traceback (most recent call last):
  File "c:\users\danie\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\danie\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\Scripts\pylanguagetool.exe\__main__.py", line 7, in <module>
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\pylanguagetool\__init__.py", line 257, in main
    print_errors(response,
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\pylanguagetool\__init__.py", line 166, in print_errors
    print(
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 187, in write_and_convert
    self.write_plain_text(text, cursor, start)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "c:\users\danie\appdata\local\programs\python\python39\lib\encodings\mbcs.py", line 25, in encode
    return mbcs_encode(input, self.errors)[0]
UnicodeEncodeError: 'mbcs' codec can't encode character '\u2717' in position 0: invalid character

并在输出文件中仅写入以下内容:

English (US) detected (99% confidence)

Possibly missing comma found.
  

关于如何进行的任何建议?

更新:在建议采用以下内容后:

pylanguagetool --no-color Text_01.txt > Text_01_comments.txt

生成以下错误:

C:\Users\danie\Desktop\Python\Grading\HI333\Test>pylanguagetool --no-color Text_01.txt > Text_01_comments.txt
Traceback (most recent call last):
  File "c:\users\danie\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\danie\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\Scripts\pylanguagetool.exe\__main__.py", line 7, in <module>
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\pylanguagetool\__init__.py", line 257, in main
    print_errors(response,
  File "c:\users\danie\appdata\local\programs\python\python39\lib\site-packages\pylanguagetool\__init__.py", line 166, in print_errors
    print(
  File "c:\users\danie\appdata\local\programs\python\python39\lib\encodings\mbcs.py", line 25, in encode
    return mbcs_encode(input, self.errors)[0]
UnicodeEncodeError: 'mbcs' codec can't encode character '\u2717' in position 2: invalid character

并继续在输出文件中写入以下内容:

English (US) detected (99% confidence)

Possibly missing comma found.
4

0 回答 0