I have a dictionary where item_dictionary[0]
corresponds to data[0]
, and item_dictionary[1]
corresponds to data[1]
.
item_dictionary = {'10350':'cat', '560':'dog'}
names = item_dictionary.values()
data = [[1, 2, 3, 4], [5, 6, 7]]
I tried to write the items in data[0]
and data[1]
to different .csv files like this:
def writer(item):
q = data.index(item)
myfile = open('%r.csv', 'wb') % names[q]
wr = csv.writer(myfile)
wr.writerows(data[q])
myfile.close()
z = [writer(x) for x in data]
This returns the error:
Traceback (most recent call last):
File "", line 1, in
File "", line 3, in writer
TypeError: unsupported operand type(s) for %: 'file' and 'str'`.
My guess for the 'str'
part of the error is that names[q]
returns, say, 'cat'
instead of cat
. However, I don't know what operand type to use instead and don't know how to get around the str
problem, so could someone please help me? Thanks.