to write set of values to a file from python
Scott David Daniels
scott.daniels at acm.org
Fri Dec 16 10:28:11 EST 2005
muttu2244 at yahoo.com wrote:
> hi
> thanks every body for the help.
> Now how can check whtr the row am reading is the last row or not??
>
> for example: ...
> reader = csv.reader(file)
> for row in reader:
> print row
> HERE HOW CAN I CHECK WHTR THIS ROW IS THE LAST ONE IN THE FILE
>
> so that if at all i dint find what am searching for i can write that
> information at the last row, after opening the file in a "append" mode.
What Steve Holden says is right. If it turns out you need to know
ahead of time, use a lagged input to determine whether it is last:
def lagged(source):
'''produce element,islast for elements in source'''
generator = iter(source)
previous = generator.next()
for element in generator:
yield previous, False
yield previous, True
file = open ('C:\some.csv','r')
reader = csv.reader(file)
for row, final in lagged(reader):
print row
if final:
print 'final:', row
else:
print 'leads:', row
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list