thouska / spotpy

A Statistical Parameter Optimization Tool
https://spotpy.readthedocs.io/en/latest/
MIT License
254 stars 152 forks source link

csv writer does not work in Python 2 #217

Closed KittelC closed 3 years ago

KittelC commented 5 years ago

Hello!

I recently updated to v. 1.5.1 and ran into the following error message (I am using Python 2.7):

In the script csv.py, the line: self.db.write(str(','.join(self.header) + '\n')) produces TypeError: write() argument 1 must be unicode, not str

The error seems to come from the line above self.db = io.open(self.dbname + '.csv', 'w')

The native open function supports string as input, or the file must be opened in "wb" mode instead of only "w" (see: https://stackoverflow.com/questions/18449233/2-7-csv-module-wants-unicode-but-doesnt-want-unicode )

thouska commented 5 years ago

Thank you for reporting this issue and I warmly appreciate your solution suggestion. I implemented it accordingly.

philippkraft commented 5 years ago

I would not suggest to follow that path. A .csv file is a text file and I see no reason to handle it as a binary file. It is the problem of Python 2, which is falling out of support soon, to handle text and binary data in an inconsistant way. I would rather remove the str(...), since the result of join is str in Python 3 and unicode or str in Python 2 anyway.

MuellerSeb commented 4 years ago

I would let this die with dropping py2 support following https://python3statement.org/

thouska commented 3 years ago

And some point I will clean up the code with all this stuff that was necessary to be able to support py2 and py3 versions. Since versions >=1.5.13 spotpy is not tested any more for py2, i.e. support was dropped.