trouble writing results to files

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Wed Nov 29 16:22:57 CET 2006


lisa.engblom at gmail.com wrote:
> import csv
> output = csv.writer(open('/Python25/working/output.csv', 'a'))
> a = ["apple", "cranberry", "tart"]
> for elem in range(len(a)):
>    output.writerow(a[elem])

output.writerow expects a sequence as an argument.  You are passing a
string, which is a sequence of characters.  By the way, what output are you
expecting to get?  Do you want a file with only one line (apple,
cranberry, tart), or each fruit in a different line?

BTW, iterating over range(len(a)) is an anti-pattern in Python.  You should
do it like this:

    for item in a:
	output.writerow([item])

> Second, there is a significant delay (5-10 minutes) between when the
> program finishes running and when the text actually appears in the
> file.

Try closing the file explicitly.
Cheers,
-- 
Roberto Bonvallet



More information about the Python-list mailing list