""" Use the encoding choice dialog to reload file in case of error. """ import sys from common import setup_editor, get_file_path from pyqode.qt import QtWidgets from pyqode.core import dialogs app = QtWidgets.QApplication(sys.argv) success = False editor = setup_editor() pth = get_file_path() success = False encoding = 'utf-8' while not success: try: editor.file.open(pth, encoding=encoding, use_cached_encoding=False) except UnicodeDecodeError: encoding = dialogs.DlgEncodingsChoice.choose_encoding( editor, pth, encoding) else: success = True app.exec_()
""" Show how decoding errors can be automatically handled using the EncodingPanel. """ import sys from pyqode.qt import QtWidgets from pyqode.core import api, panels from common import setup_editor, get_file_path app = QtWidgets.QApplication(sys.argv) editor = setup_editor( panels=[(panels.EncodingPanel(), api.Panel.Position.TOP)]) editor.file.open(get_file_path(), use_cached_encoding=False) app.exec_()
This example tries to detect the file encoding using chardet. You can install chardet by running the following commmand:: pip install chardet .. note:: If you run this example without supplying a file argument, it will open a fallback file encoded with a chinese encoding. Chardet will not detect the right encoding but the file will load without any errors. I made this example to show you that you should not rely on a charset detector but instead provided a way for the user to reload the file with another encoding. This can be done using the EncodingMenu (in pyqode.core.widgets). """ import sys from pyqode.qt import QtWidgets from pyqode.core import api, panels, widgets from common import setup_editor, get_file_path import chardet pth = get_file_path() with open(pth, 'rb') as buf: encoding = chardet.detect(buf.read())['encoding'] app = QtWidgets.QApplication(sys.argv) editor = setup_editor(panels=[(panels.EncodingPanel(), api.Panel.Position.TOP)]) editor.file.open(pth, encoding=encoding, use_cached_encoding=False) app.exec_()
""" Show how decoding errors can be automatically handled using the EncodingPanel. """ import sys from pyqode.qt import QtWidgets from common import setup_editor, get_file_path app = QtWidgets.QApplication(sys.argv) editor = setup_editor() editor.file.open(get_file_path(), use_cached_encoding=False) app.exec_()