I want to read in a list of numbers from a file as chars one char at a time to check what that char is, whether it is a digit, a period, a + or -, an e or E, or some other c
Here is a technique to make a one-character-at-a-time file iterator:
from functools import partial
with open("file.data") as f:
for char in iter(partial(f.read, 1), ''):
# now do something interesting with the characters
...
f.read(1)
. for x in open()
reads lines from a file. Read the entire file in as a block of text, then go through each character of the text:
import sys
def is_float(n):
state = 0
src = ""
ch = n
if state == 0:
if ch.isdigit():
src += ch
state = 1
...
data = open("file.data", 'r').read()
for n in data: # characters
sys.stdout.write("%12.8e\n" % is_float(n))
In fact it's much easier. There is a nice utility in itertools, that's often neglected. ;-)
for character in itertools.chain.from_iterable(open('file.data')):
process(character)