I would like to change encoding of .txt files in a directory with python to UTF-8, are there ways to do that ?
Thank you for your support.
I have viewed the solution already mentionned by stackoverflow users here : How to convert a file to utf-8 in Python?
I would like to apply it for all files of particular category in the directory and not one file.
import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile:
with codecs.open(targetFileName, "w", "utf-8") as targetFile:
while True:
contents = sourceFile.read(BLOCKSIZE)
if not contents:
break
targetFile.write(contents)
1) I would like to change encoding of files in a directory to UTF-8, I know the input encoding.
2) are there solutions to transform to UTF-8 without knowing the input encoding ? ( not important at this time, but if a solution already exist, it will be great to know about it)