pyinstaller的文本无法解码

时间:2018-09-27 13:01:48

标签: python python-3.x encoding compilation pyinstaller

我尝试从.txt文件中提取文本,但是收到错误消息:

ERROR:root:decode error:
Traceback (most recent call last):
  File "ml_funcs/tokenizer.py", line 15, in extract_text
  File "textract/parsers/__init__.py", line 77, in process
  File "textract/parsers/utils.py", line 46, in process
  File "textract/parsers/txt_parser.py", line 9, in extract
  File "/Users/ivanlavrenov/projects/project/.venv2/lib/python3.7/codecs.py", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 23: invalid start byte
  

pyinstaller出于未知原因无法使用open(file.txt)

解码文本

当尝试在其他计算机上启动.exec时,textract无法解码任何文本。 hiddenimports没有帮助。有我的.spec文件:

# -*- mode: python -*-
import sys
from PyInstaller.utils.hooks import collect_data_files

block_cipher = None

a = Analysis(['main.py'],
             pathex=['/Users/ivanlavrenov/projects/project'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

a.datas += [('messages.properties',
             'messages.properties', 'DATA'), ]

a.datas += Tree('.venv2/lib/python3.7/site-packages/langdetect/profiles/',
                prefix='langdetect/profiles/')

a.datas += Tree('.venv2/lib/python3.7/site-packages/stop_words/stop-words/',
                prefix='stop-words/')

a.datas += Tree('./desktop_app/images/', prefix='desktop_app/images/')

a.hiddenimports.append("textract.parsers")
a.hiddenimports.append("docx2txt")
a.hiddenimports.append("csv")
a.hiddenimports.append("xlrd")
a.hiddenimports.append("chardet")
a.hiddenimports.append("codecs")

print(a.hiddenimports)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='main',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True,
          icon='desktop_app/images/icon.icns')

如果有人有任何想法,这对我非常有帮助)

1 个答案:

答案 0 :(得分:0)

通过添加编码的chardet检测已解决了该问题