Reputation: 1
Here is my problem:
I have a dictionary.
dictionary = {"key1": "value1", "key2": "value2", "key3": value3}
I'm trying to write the dictionary keys in a row and the corresponding values in the following row.
with open("file.csv") as f:
datawriter = csv.writer(f)
for i in dictionary:
datawriter.writerows([i])
datawriter.writerows([dictionary[i]])
When I open the csv file, the letters for the keys and their corresponding values occupy separate cells. So it looks like this: |k|e|y|1| or |v|a|l|u|e|. This isn't what I want. I want the keys and their corresponding values to occupy entire cells like this: | key1 | key2 | or | value1 | value2 |.
The reason I'm doing this is because I want to format the csv file so it has two columns, one for the keys and the other for their corresponding values.
I've looked through Stack and people have advised putting a list into writerows(), which I have done, but that doesn't work when I'm using the for loop. There is a solution that says to do writerows([dictionary.keys()]), and that print all the keys in separate cells, but across a single row. I don't want that.
Any fixes?
Upvotes: 0
Views: 3536
Reputation: 469
for writing a dictionary to csv file you can use dictionary iterator. the dict.items() function returns a key, value pair as a tuple. csvwriter.writerow() expects a list/tuple.
import csv
dictionary = {"key1": "value1", "key2": "value2", "key3": "value3"}
with open("file.csv","wb") as csv_file:
csv_writer = csv.writer(csv_file)
for key, value in dictionary.items():
csv_writer.writerow([key,value])
csv_file.close()
Alternatively you can pass the tuple too.
import csv
dictionary = {"key1": "value1", "key2": "value2", "key3": "value3"}
with open("file.csv","wb") as csv_file:
csv_writer = csv.writer(csv_file)
for key_value_tuple in dictionary.items():
csv_writer.writerow(key_value_tuple)
csv_file.close()
Upvotes: 0
Reputation: 37344
The reason I'm doing this is because I want to format the csv file so it has two columns, one for the keys and the other for their corresponding values.
If you don't care about the order your rows appear in the dict, this can be done by passing iteritems
to writerows
. Or items
on Python 3.x.
with open("file.csv") as f:
datawriter = csv.writer(f)
datawriter.writerows(dictionary.iteritems())
If you do care about order, you should be able to use sorted
on dictionary.iteritems()
but you might need to specify a custom sort key function depending on what sorting behavior you need.
writerows
expects an iterable that contains one entry for each row to be written. Each entry should itself be iterable, yielding one entry for each column to be written. Since you want each key/value pair to be on a separate row, the main iterable should yield one iterable for each key/value pair in the dictionary, and iteritems
does exactly that.
You're seeing unwanted behavior in your attempted solution because you're calling writerows
with a single-dimensioned list. There's one element in your list, so it writes one row, but the iterable yielded by the main iterable is a simple string. Iterating over the string gives you each character, which is why each is appearing in a separate column.
Upvotes: 2