[issue40762] Writing bytes using CSV module results in b prefixed strings

Rémi Lapeyre report at bugs.python.org
Mon May 25 05:46:23 EDT 2020


Rémi Lapeyre <remi.lapeyre at henki.fr> added the comment:

> As an example, if I write character "A" as a byte, i.e b'A' in a csv file

But you can't write b'A' in a csv file, what you can't do is write `b'a'.decode()` or `b'a'.decode('latin1')` or `b'a'.decode('whatever')` but the string representation of a byte string is dependant on the character encoding and it's not possible to guess it, which is why bytes and string were separated in Python3 in the first place.

Since csv can't write bytes, it gets a string representation of the by calling str(), as it would with any other objects.

> which is not what the user would have wanted in majority of the use-cases

If you try to guess the encoding, you will obligatory fail in some case and the resulting file will be corrupted. The only way here is for your users to fix their program and decode thee byte string using the correct encoding before giving them to csv.

----------
nosy: +remi.lapeyre

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40762>
_______________________________________


More information about the Python-bugs-list mailing list