trouble writing results to files
lisa.engblom at gmail.com
lisa.engblom at gmail.com
Wed Nov 29 11:18:59 EST 2006
Roberto Bonvallet wrote:
> 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?
I want it to print everything on one line and then create a new line
where it will print some more stuff. In my real program I am iterating
and it will eventually print the list a couple hundred times. But it
would be useful to understand how to tell it to do either.
> 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])
I can try that. Is using range(len(a)) a bad solution in the sense
that its likely to create an unexpected error? Or because there is a
more efficient way to accomplish the same thing?
thanks!
Lisa
More information about the Python-list
mailing list