As the question described, I want to read a text file character by character.
I have a large file that is mostly text but also contains some illegitimate bytes that is not accepted by Python, currently I don't have the time to figure out what is actually wrong, so I just want to skip all the problematic bytes using try
.
with open(filein,'r',encoding='ascii') as file:
while True:
try:
char = file.read(1)
except UnicodeDecodeError:
continue
if not char:
break
print(char)
However this doesn't work as it just skip over all the bytes and outputs nothing.
My instinct thinks that it's because everytime I call READ it reads the file entirely before cropping it, and considers it as an Error.
So I was wondering if there is a way to literally read a single char out of a file in Python, kinda like fgetc()
in C?