Reputation: 321
I have a list in python with 4 elements that are all themselves lists of lines ( text)
I want to save those elements in numbered filenames.
for n in textblock:
for line in textblock[n]:
with open('file_{0}.dat'.format(n),'w') as ffile:
ffile.write(textblock[n[line]])
ffile.close()
I get the error message :
for line in textblock[n]:
TypeError: list indices must be integers, not list
Any hint what i can do to fix it ?
Upvotes: 2
Views: 1051
Reputation: 180391
If you want to write lists of lists use the csv module:
import csv
for ind, lsts in enumerate(textblock):
with open('file_{0}.dat'.format(ind),'w') as ffile:
wr = csv.writer(file)
wr.writerows(lsts)
Upvotes: 0
Reputation: 738
Let's clarify your data structure. It sounds like this:
outer_list = [ ["STRING1", "STRING2", ...],
["STRING1", "STRING2", ...],
["STRING1", "STRING2", ...],
["STRING1", "STRING2", ...]
]
So let's use python's iterable for loop structure to traverse the outer list.
for inner_list in outer_list:
But we want to write the contents of each inner_list
to a file, using the position of the inner_list
in the outer_list
. Let's use the built in enumerate()
function to get the position.
for n, inner_list in enumerate(outer_list, 1):
# write inner list to file
Let's examine writing the inner_list
to a file.
with open('file_{0}.dat'.format(n),'w') as ffile: # this is fine, well done.
ffile.write(inner_list)
All together that would be:
for n, inner_list in enumerate(outer_list, 1):
with open('file_{0}.dat'.format(n),'w') as ffile:
ffile.write(inner_list)
# no need to close the file, the with syntax does this for you.
EDIT: There is one more step needed here, as pointed out by @mhawke
The write()
method requires it to be passed a string. We can convert our inner_list to a string quite easily. My preferred method would be to use the join()
method. We can specify that we join each string in inner_list
with a new line character, so that it will print nicely, i.e.
'\n'.join(inner_list)
Therefore our final solution would be:
for n, inner_list in enumerate(outer_list, 1):
with open('file_{0}.dat'.format(n),'w') as ffile:
ffile.write('\n'.join(inner_list))
Upvotes: 1
Reputation: 59095
If you are iterating through your textblock
with for n in textblock:
, then n
will take the value of each item in textblock
(so n
is a list
). Then if you try and get textblock[n]
, then you are trying to use the list n
as an index.
You can just have:
for x in textblock:
for line in x:
# do stuff with the line
Don't try and close ffile
-- it is closed automatically by the with
block.
If you want to use the index in the file name, I think what you want is something like this:
for i, lines in enumerate(textblock):
with open('file_{0}.dat'.format(i),'w') as ffile:
for line in lines:
ffile.write(line)
enumerate
will give you the index and the items as you iterate through the list.
Upvotes: 1
Reputation: 20339
You can use enumerate
for n,m in enumerate(textblock, 1):
with open('file_{0}.dat'.format(n),'w') as ffile:
for line in m:
ffile.write(line)
Note
with open
will take care of close()
.for i in [1,2,3]
gives 1,2,3
in each iteration
.No need of [1,2,3][i]
Upvotes: 1