Possible Duplicate:
subprocess.Popen.stdout - reading stdout in real-time, again!
I am processsing the ouptut of a file in binary but I am using a temporary string to represent the output. Since the output could be in theory fairly large, I would prefer to process the output as a stream using unpack or unpack_from.
The code is something like this:
file = '/home/t/FinancialData/GBPUSD/2007/05/01/20070501_01h_ticks.bi5';
command = ('lzma', '-kdc', '-S', 'bi5', file);
p = subprocess.Popen(command, stdout=subprocess.PIPE);
out, err = p.communicate();
for s in (out[x:x+20] for x in range(0, len(out), 20)):
values = struct.unpack(">3L2f", s)
with open(csvfilename, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(values);
Is there a way to rewrite this so it does not have to store the whole output in out but process it as a stream ?