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 :)