Python has no built-in construct for this. You can write a generator that reads the characters one at a time and accumulates them until you have the entire selected item.
def items(infile, delim): item = [] c = infile.read(1) while c: if c == delim: yield "".join(item) item = [] else: c = infile.read(1) item.append(c) yield "".join(item) with open("log.txt") as infile: for item in items(infile, ","):
You will get better performance if you read the file in chunks (say 64K or so) and separate them. However, the logic for this is more complicated, since the element can be divided into pieces, so I will not go into it here, since I'm not sure that everything is correct. :-)
source share