Anonymous user
Read a file character by character/UTF8: Difference between revisions
Read a file character by character/UTF8 (view source)
Revision as of 21:39, 10 June 2014
, 10 years ago→{{header|Python}}
(→{{header|AutoHotkey}}: Created AutoHotkey entry) |
|||
Line 398:
=={{header|Python}}==
{{works with|Python|2.7}}
<lang python>
def get_next_character(f):
with open(filename,"rb") as f:▼
# note: assumes valid utf-8
while True:▼
c
while
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:
for c in get_next_character(f):
print(c)
</lang>
|