[Tutor] [Fwd: Re: reading and writing with csv then appending some data to a specific line]

Kent Johnson kent37 at tds.net
Mon Nov 5 15:11:50 CET 2007


Forwarding to the list...

-------- Original Message --------
Subject: 	Re: [Tutor] reading and writing with csv then appending some
data to a specific line
Date: 	Mon, 5 Nov 2007 14:54:49 +0100
From: 	pierre cutellic <pierre.cutellic at gmail.com>
To: 	Kent Johnson <kent37 at tds.net>
References:
<3c8b20230711040801i508bed0aq47eb18d268664e7b at mail.gmail.com>
<472E77EE.50908 at tds.net>



many thanks,
but i don't really want to erase the content of the file i'm writing to.
This script was a first step for me to understand how to write to a file
at a given line of the file without erasing its content. I didn't really
understood the code you linked me:
http://mail.python.org/pipermail/python-list/2000-August/048839.html .
when i'm using seek(), data is appended to the end of the file.:(

On Nov 5, 2007 2:54 AM, Kent Johnson <kent37 at tds.net
<mailto:kent37 at tds.net>> wrote:

     pierre cutellic wrote:
      > Hi,
      > I wrote this script to select some data from a csv file.
      > Actually it writes it into a file but i would like to append it
     into an
      > existing one to a specific line without removing existing code.
      >  does the open(' filename.csv','a+') can do that?
      >
      >         ag = elem[0]
      >         ag.split(";")

     Here I think you want
       ag = ag.split(';')
     split() does not split the string in place, it returns a new list with
     the split elements.

     Also you are opening the file for writing (which will erase the
     contents) before you read it.

     Other than that your code looks OK. You might want to try writing it
     without functions, I think they may be confusing you a bit and they are
     not really needed here.

     Kent




-- 
Pierre Cutellic____________
OO   33   6   15   67   17   85
___pierre.cutellic at gmail.com <http://gmail.com>


More information about the Tutor mailing list