This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author afflictor
Recipients afflictor
Date 2019-11-06.10:04:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573034688.04.0.163382933915.issue38717@roundup.psfhosted.org>
In-reply-to
Content
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]
History
Date User Action Args
2019-11-06 10:04:48afflictorsetrecipients: + afflictor
2019-11-06 10:04:48afflictorsetmessageid: <1573034688.04.0.163382933915.issue38717@roundup.psfhosted.org>
2019-11-06 10:04:47afflictorlinkissue38717 messages
2019-11-06 10:04:47afflictorcreate