T
T
TemaKam2021-05-07 12:43:31
Python
TemaKam, 2021-05-07 12:43:31

How to make it so that there are no encoding errors when pasted into a QTextBrowser?

I insert lines from a file into qtextbrowser, then I save them to a file like this (when changing qtextbrowser)

self.accounts = []
lists = self.all_accounts_task.toPlainText().split('\n')
for l in lists:
        if l != '' and l != '\n':
            self.accounts.append(l)
self.all_accs_task.setText(str(len(self.accounts)))
cfg = ConfigParser()
cfg.read('settings/default/tasks.ini', encoding='utf-8')
cfg[self.task_name].update({'accs': '\n'.join(self.accounts).replace('%', '%%')})
cfg[self.task_name].update({'all_accs_task': str(len(self.accounts))})
with open('settings/default/tasks.ini', 'w', encoding='utf-8') as setfile:
        cfg.write(setfile)

that is, I specify utf-8 everywhere, but if I insert a line that looks like this,
MQnwDKU.jpg
then in qtextbrowser it will be inserted somehow, UwdAXpw.jpg
but when saving to a file, I already get an error
UnicodeEncodeError: 'utf-8' codec can't encode character '\uda36' in position 20: surrogates not allowed

but in fact there are such characters: iDkO7aO.jpg
how to make such characters inserted into qtextbrowser immediately normally (in Russian)?

qtextbrowser settings:
GgmA2gs.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yupiter7575, 2021-05-07
@yupiter7575

use io library:

import io
with io.open('test.txt', 'w') as f:
    pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question