Read a file character by character/UTF8: Difference between revisions

(→‎{{header|AutoHotkey}}: Created AutoHotkey entry)
Line 398:
=={{header|Python}}==
{{works with|Python|2.7}}
{{incorrect|Python|It reads the file byte by byte not character by character.}}
<lang python>
def get_next_character(f):
with open(filename,"rb") as f:
# note: assumes valid utf-8
while True:
c onebyte= f.read(1)
while Truec:
if not onebyte:
while breakTrue:
byte=onebyte[0] try:
yield c.decode('utf-8')
except UnicodeDecodeError:
# we've encountered a multibyte character
# read another byte and try again
c += f.read(1)
else:
# c was a valid char, and was yielded, continue
c = f.read(1)
break
 
# Usage:
with open(filename"input.txt","rb") as f:
for c in get_next_character(f):
print(c)
</lang>
 
Anonymous user