[Python-3000] encoding hell

Oleg Broytmann phd at mail2.phd.pp.ru
Wed Sep 6 12:37:51 CEST 2006


On Tue, Sep 05, 2006 at 06:09:21PM -0700, Guido van Rossum wrote:
> On 9/4/06, Oleg Broytmann <phd at oper.phd.pp.ru> wrote:
> >--- email database file ---
> > phd at phd.pp.ru
> > phd at oper.med.ru
> >--- / ---
> >
> >   The program opens the file in "r+" mode, reads it line by line and
> >stores the positions of the first character in an every line using tell().
> >When it needs to mark an email it seek()'s to the stored position and write
> >'+' mark so the file looks like
> >
> >--- email database file ---
> >+phd at phd.pp.ru
> > phd at oper.med.ru
> >--- / ---
> 
> I don't understand how it can insert a character into the file without
> rewriting everything after that point.

   The essential part of the program is:

results = open("results", "r+")
name, email = getaddresses([to])[0]

while 1:
   pos = results.tell()
   line = results.readline()
   if not line: break

   if line.strip() == email:
      results.seek(pos)
      results.write('+')
      break

results.close()

   Open the "database" file in "r+" mode, find the email, seek to the
beginning of the line, replace the space with '+'.

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.


More information about the Python-3000 mailing list