writing to file very slow

John Machin sjmachin at lexicon.net
Wed Mar 26 23:01:46 CET 2003


Jack Diederich <jack at performancedrivers.com> wrote in message news:<mailman.1048694268.32605.python-list at python.org>...
> On Wed, Mar 26, 2003 at 03:54:20PM +0100, Moritz Lennert wrote:
> >   for x in range(results.ntuples()):
> >    var = ""
> >    for y in range(len(results.listfields())-1):
> >      var+=str(results.getresult()[x][y])+'|'
> >    var+=str(results.getresult()[x][len(results.listfields())-1])
> >    f.write(var)
> >    f.write('\n')

>   gotten_result = results.getresult() # we only call this once
>   field_str = '|'.join(map(str, gotten_result[0]))
>   for row in gotten_result:
>     f.write(field_str)
>     f.write(row[-1]) # is this really what you want? translation of below line
>     # var+=str(results.getresult()[x][len(results.listfields())-1])
>     f.write('\n')

No, it's not what he wanted, and it's not a correct translation but
the original is so god-awfully-obscure that you are forgiven :-)

Here's his original, with two substitutions, and removing the str() --
this makes it much clearer what is going down ...

   for row in gotten_result:
      var = ""
      for y in range(numfields-1):
         var += row[y] + '|'
      var += row[numfields-1]
      f.write(var)
      f.write('\n')




More information about the Python-list mailing list