Writing to files
markx.daley at intel.com
Mon Mar 20 23:42:48 CET 2000
Daley, MarkX wrote:
> Here is a piece of code I wrote to write the items in a list sequentially
> a file.
> a=some list ([1,2,3,whatever])
> for x in a:
> When I run it, I get this:
> Traceback (innermost last):
> File "<pyshell#2>", line 1, in ?
> File "C:\PROGRA~1\Python\collect.py", line 30, in collect
> TypeError: sequence index must be integer
Of course. The first thing you want is:
for x in a:
at least until you discover that write wants a string (which may
> Any ideas on what is causing this? The data in the list is columnar
> information pulled from a database, but this shouldn't be the cause of the
> problem because I can substitute a print statement in for the f.write
> statement and it works just fine. Any suggestions are appreciated.
No, "print a[x]" would have the same problem. "print x"
wouldn't, but it automatically converts x to a string if it's not
I didn't realize print converts to a string. I guess the question I should
ask then, is how do I write a list to a file item by item?
More information about the Python-list