New submission from Patrick Schneeweiß <[email protected]>:
I switched over to Python 3.7 and still using my csv DictWriter.
No I'm receiving an error when calling the writerow() method.
unsupported operand type(s) for -: 'list' and 'list'
Digging deeped in the problem I found out, that the internal
_dict_to_list(self, rowdict) method works different compared to Python 2.7
It tries to substract the fieldnames list and the keys of the dict to find keys
not included.
But substracting lists is not possible. As a workaround I defined the
Dictwriter with the option extrasaction="ignore".
I would be better to use the method like in Python 2.7. I will output my
versions of the method:
Python 3.7:
def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
wrong_fields = rowdict.keys() - self.fieldnames
if wrong_fields:
raise ValueError("dict contains fields not in fieldnames: "
+ ", ".join([repr(x) for x in wrong_fields]))
return (rowdict.get(key, self.restval) for key in self.fieldnames)
Python 2.7:
def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
wrong_fields = [k for k in rowdict if k not in self.fieldnames]
if wrong_fields:
raise ValueError("dict contains fields not in fieldnames: "
+ ", ".join([repr(x) for x in wrong_fields]))
return [rowdict.get(key, self.restval) for key in self.fieldnames]
----------
components: Library (Lib)
messages: 356110
nosy: afflictor
priority: normal
severity: normal
status: open
title: csv DictWriter's internal _dict_to_list raise error unsupported operand
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38717>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com