1

so far I got managed to get this code running:

# -*- coding: utf-8 -*-

import sys,os
from tkinter import Tk
from tkinter.filedialog import askopenfilename



def window():
    root = Tk()
    root.withdraw()
    ftypes = [('Subtitle file',"*.srt"),('Subtitle file',"*.sub"),('Text file',"*.txt")]
    ttl  = "Open"
    dir1 = 'D:\\ovdjetorent'
    root.fileName = askopenfilename(filetypes = ftypes, initialdir = dir1, title = ttl)
    TitlFajlPath = str(root.fileName)
    return TitlFajlPath

def radnja_sa_fajlom():
    TitlFajlPath = window()
    print(TitlFajlPath)
    TitlFajl = open(TitlFajlPath, 'r+')
    TitlContent = TitlFajl.read()
    TitlContent = TitlContent.replace('æ','ć').replace('è','č').replace('ð','đ').replace('Æ','Ć').replace('È','Č')
    TitlFajl.write(TitlContent) 
    print(TitlContent)



    input('Press ENTER to exit')

radnja_sa_fajlom()

The function window() is just a fancy way of getting a subtitle file path of the file I'll be working with. The function radnja_sa_fajlom() opens that file with os.open() function, it changes the bad characters, but when I attempt to save it, it fails with:

UnicodeEncodeError: 'charmap' codec can't encode character '\u0107' in position 608: character maps to <undefined>

I tried using codecs and io modules, but they ignore newlines and I end up with jumbled text.

What should I do to write the replaced content to the same file In the UTF-8 encoding, without any issues?

Thanks :)

  • By explicitly specifying the encoding to use. You used `open()` without `encoding='utf8'`, so a system default is used. – Martijn Pieters Oct 29 '18 at 22:06
  • @MartijnPieters This doesn't seem to solve the problem unfortunately, as I still end up with the UnicodeEncodeError I mentioned in the question – A random dood Oct 29 '18 at 22:34
  • You'll need to add a full traceback of the exception then. Is it the `print()` line? Then see [Python, Unicode, and the Windows console](//stackoverflow.com/q/5419); if you upgrade to Python 3.6 or newer that issue is resolved. – Martijn Pieters Oct 30 '18 at 12:06

0 Answers0