CSV Issues

Lutz Horn dev-random at arcor.de
Thu Jul 19 08:39:38 CEST 2007


Hi,

On Thu, 19 Jul 2007 06:59:24 +0200, Rohan <vodela.sai at gmail.com> wrote:
> When I run the script for the second time after a certain period of
> time the results should appear next to the results of the last run,
> I'm unable to make a new column when the script is run after the first
> time.
> Ideally I would like to have an output which looks like this.
> 1/20       1/27
> we.py    we.py
> gh.py    gj.py   <- Indicating tht the file has changed
> fg.py    fg.py

Try something like this:

>>> import csv
>>> f = open("/tmp/data.csv", "rb")
>>> reader = csv.reader(f)
>>> headings = reader.next()
>>> headings
['1/20']
>>> rows = []
>>> for row in reader:
...     rows.append(row)
...
>>> rows
[['we.py'], ['gh.py'], ['fg.py']]
>>> f.close()
>>> headings.append("1/27")
>>> rows[0].append("we.py")
>>> rows[1].append("gj.py")
>>> rows[2].append("fg.py")
>>> f = open("/tmp/data.csv", "wb")
>>> writer = csv.writer(f)
>>> writer.writerow(headings)
>>> writer.writerows(rows)
>>> f.close()

Regards
Lutz



More information about the Python-list mailing list