Closed rgaiacs closed 9 years ago
What were the issues that came up? And is it possible to make the script 2-and-3-compatible? G
What were the issues that came up?
Traceback (most recent call last):
File "bin/update-team.py", line 68, in <module>
write_team(contribs)
File "bin/update-team.py", line 57, in write_team
file_.write(u"<tr>\n<td>{0}</td>".format(contributor))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
The script is available at https://github.com/r-gaia-cs/bc/blob/team/bin/update-team.py
And is it possible to make the script 2-and-3-compatible?
Probably. I just need to figure out how write unicode char to file. The unicode came from subprocess.check_output
.
What type is contributor
when this errors under Python 2?
Here's an example that writes unicode to a file with Python 2 & 3 assuming that the input is bytes
:
from __future__ import print_function
import tempfile
s = b'\xe0\xa5\x90' # om
to_write = u'{}'.format(s.decode('utf-8'))
print(to_write)
file_ = tempfile.TemporaryFile()
file_.write(to_write.encode('utf-8'))
The key there is decoding the bytes to unicode, doing stuff, then encoding back to bytes before writing it out.
What type is
contributor
when this errors under Python 2?
str
Thanks for the example but I didn't solve the problem yet.
My example should work if contributor
is str
in Python 2, but it will not work if contributor
is also str
in Python 3, it needs to be bytes in both places.
We will switch to Python 3 after the lesson repo has exploded for both teaching and tools.
When working at #578 I had some issues when testing my script with Python2 due UTF-8. Is acceptable to require the users of some tools at
bin
to use Python3?