El dc 22 de 11 del 2006 a les 11:04 +0100, en/na
[EMAIL PROTECTED] va escriure:
> Hi,
>
> Is there a fast way to 'export' a complete table to a text file? Now I am
> iterating over rows with iterrows() and then use a "for colname in
> table.colnames" loop to write each individual value. But this is relatively
> slow, and I was wondering if someone could give me advice on how to speed
> things up.
Yes, it is slow, but I can't figure out a faster way to do it. In fact,
this method is quite faster than using a recarray in-memory and only
somewhat slower than native Python printing. Consider this:
import tables
import numpy
from time import time
N=10000
recarray = numpy.zeros(shape=N, dtype='i4,f4')
f = tables.openFile('/tmp/test.h5', 'w')
t = f.createTable('/', 'table', recarray)
ofile = file('/tmp/out.txt', 'w')
t1=time()
for row in t:
for col in t.colnames:
ofile.write(str(row[col])+'\t')
ofile.write('\n')
print "time (original)-->", time()-t1
ofile.close()
ofile = file('/tmp/out2.txt', 'w')
t1=time()
for row in recarray:
for col in ['f0','f1']:
ofile.write(str(row[col])+'\t')
ofile.write('\n')
print "time (recarray)-->", time()-t1
ofile.close()
ofile = file('/tmp/out3.txt', 'w')
t1=time()
for row in xrange(N):
ofile.writelines(str(row)+'\t'+str(float(row)))
ofile.write('\n')
print "time (native)-->", time()-t1
ofile.close()
f.close()
The output for my machine is:
time (original)--> 0.402892112732
time (recarray)--> 1.19862508774
time (native)--> 0.267643928528
So, I guess the main bottleneck here is in formatting strings in native
Python.
Cheers,
--
Francesc Altet | Be careful about using the following code --
Carabos Coop. V. | I've only proven that it works,
www.carabos.com | I haven't tested it. -- Donald Knuth
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Pytables-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pytables-users