What is "not staying right"? If you don’t show what’s wrong, there’s no way to say right.
In fact, "ANSI encoding" isn’t even a standard - in Python, it’s only valid on Windows, and gives error on other systems, and indicates the standard Windows encoding the program is running on. Take your same program, same data file, and run a C in Ukraine, and get different results! Since you know what encoding the files are in (if they are shown correctly on a Windows machine), use the fixed encoding name: "latin-1". (Hence, if you take your data and the same program to another PC, one in the cloud, for example, it still runs correctly).
Anyway, putting "errors='ignore'" there is a bad practice - and it won’t amount to anything: for "latin-1" encoding all bytes are valid, so there will never be a Decoding error, and, on the other hand, for UTF-8 encoding, all Unicode characters are valid, then you’ll never make a mistake either. but if were there some error, all that would be achieved with ignore is that you would have data omitted from the output, and would never know about it - the characters would simply be suppressed, without any notification from the program, or anything. See this example forcing accented characters into an encoding that does not accept them:
In [4]: "maçã".encode("ASCII", errors="ignore")
Out[4]: b'ma'
Finally, but not sure, since you don’t say what is wrong, what I can imagine is that some of the files you are trying to contact for UTF-8 already are in utf-8. In this case, you will get a double encoding - accented characters will appear as two characters (if you had a sample of how the error is, you could be sure of that). Either that, or, anyway, the input files are encoded differently than expected.
In case some files are already in utf-8, a simple way to avoid double encoding is to read the file in utf-8 before, and only in case of error read the file with latin-1 encoding -
You can do:
from pathlib import Path
def encode_files(self):
folder = Path(self.config.path_prepared_scd)
for path_files in folder.glob("*.txt"):
output = folder / (path_files.stem + ".tmp")
try:
data = path_files.read_text(encoding="utf-8")
except UnicodeDecodeError:
data = path_files.read_text(encoding="latin-1")
output.write_text(data, encoding="utf-8")
The use of pathlib.Path also facilitates the handling of file names and directories - as well as providing the methods read_text
and write_text
, that for cases where the file will be transferred all at once, they are simpler than using "open", "with", etc...
Thank you very much. It worked perfectly and the code got much cleaner. I didn’t know this procedure. Thanks again @jsbueno
– Cleber Nandi