Reputation: 449
There are a lot of posts here about outputting the contents of a dictionary. I actually figured out a non-optimal solution to my problem (as I'll show below), but I'm most interested in why my non-optimal solution worked.
Here is my code:
import csv
keys = ("Ev", "E1", "E2", "E3", "I", "B", "O", "T")
p = input("P")
e = input("e")
e2 = input("e2")
e3 = input("e3")
it = input("it")
b = input("b")
o = input("o")
t = input("t")
dictionary = {"Ev": p, "E1": e, "E2": e2, "E3": e3, "I": it, "B":b, "O": o, "T": t}
dictionary2 = {"Ev": p, "E1": e, "E2": e2, "E3": e3, "I": it, "B":b, "O": o, "T": t}
with open("infile.csv", "a") as f:
w = csv.DictWriter(f, keys)
w.writeheader()
for answer in (dictionary, dictionary2):
w.writerow(answer)
My goal was to just have one dictionary. But when I tried outputting the contents of the dictionary, I kept getting a ValueError regarding "B". I tried a few solutions and the best one seemed to be adding a second dictionary which just prints the same exact output twice and I can remove exact duplicates from the output file when I open it. I know this is terrible practice, but it was the quickest solution I had and it was a bummer to get caught up in this for a while.
Why does adding a second dictionary work in this case? I don't understand why this code throws a ValueError if only one dictionary is used, but when two are it works fine (except for the duplicate line I then have to remove).
EDIT
The non-working code was the line:
for answer in (dictionary):
which, as Martijn kindly points out, just loops over the keys of the one dictionary.
EDIT2
And, the traceback error is:
Traceback (most recent call last):
File "file.py", line 41, in <module>
w.writerow(answer)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/csv.py", line 153, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/csv.py", line 149, in _dict_to_list
+ ", ".join(wrong_fields))
ValueError: dict contains fields not in fieldnames: B
Upvotes: 0
Views: 222
Reputation: 1123850
You created a tuple to loop over:
for answer in (dictionary, dictionary2):
Before, you probably tried to loop over the dictionary:
for answer in dictionary:
or perhaps:
for answer in (dictionary):
which won't work; you are then looping over just the keys of one dictionary. Since you didn't include the code that didn't work, we can only speculate on that point.
Either write the one dictionary directly without looping:
w.writeheader()
w.writerow(dictionary)
or use a one-element tuple:
for answer in (dictionary,):
w.writerow(answer)
See Tuple syntax to learn why the extra comma is essential there. Parenthesis around an expression are optional in Python, and just group that expression to span lines or denote precedence; you need at least one comma to make it a tuple.
On the other hand, a NameError
almost always points to a simple typo; perhaps you forgot to quote the "B"
key in your dictionary. Again, without a traceback, this is pure speculation.
Upvotes: 4