Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No isinstance unicode in Python3 #51

Closed
DenisCarriere opened this issue Mar 22, 2015 · 6 comments
Closed

No isinstance unicode in Python3 #51

DenisCarriere opened this issue Mar 22, 2015 · 6 comments

Comments

@DenisCarriere
Copy link

The isinstance statement with unicode does not exist in python3:

    if isinstance(s, unicode):

Here's how I fixed it in my versions, but I still don't know if it's actually the best way to test unicode so I won't do a pull request unless someone can tell me it's actually correct.

# Example ONLY
if sys.version_info.major == 2:
    if isinstance(value, (str, unicode)):  # noqa
        return value

elif sys.version_info.major == 3:
    if isinstance(value, str):
        return value

Python3 error

Traceback (most recent call last):
File "/home/denis/Github/ACAPS-tools/parser.py", line 166, in
export('Guinea_Locations_Export.csv', ['google', 'bing', 'osm'])
File "/home/denis/Github/ACAPS-tools/parser.py", line 124, in export
writer.writeheader()
File "/usr/local/lib/python3.4/dist-packages/unicodecsv/init.py", line 158, in writeheader
self.writerow(header)
File "/usr/lib/python3.4/csv.py", line 153, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/usr/local/lib/python3.4/dist-packages/unicodecsv/init.py", line 86, in writerow
return self.writer.writerow(_stringify_list(row, self.encoding, self.encoding_errors))
File "/usr/local/lib/python3.4/dist-packages/unicodecsv/init.py", line 51, in _stringify_list
return [_stringify(s, encoding, errors) for s in iter(l)]
File "/usr/local/lib/python3.4/dist-packages/unicodecsv/init.py", line 51, in
return [_stringify(s, encoding, errors) for s in iter(l)]
File "/usr/local/lib/python3.4/dist-packages/unicodecsv/init.py", line 41, in _stringify
if isinstance(s, unicode):
NameError: name 'unicode' is not defined

@tsroten
Copy link

tsroten commented Mar 22, 2015

It might be cleaner to keep that conditional statement as is. At the top of the module you could do this:

is_python3 = sys.version_info.major == 3
if is_python3:
    unicode = str

Then, all references to unicode will work properly.

@DenisCarriere
Copy link
Author

@tsroten Oh sweet, I've never thought of that! that looks good!

Thanks, I know that unicodecsv isn't really testing for Python3, but I'll incorporate in my code 👍

@jackmaney
Copy link

This is probably a bit of a newb question, especially since I'm just making the transition from Python 2 to 3, but doesn't Python 3's csv handle unicode?

If so, then you can just do:

import six

if six.PY3:
    import csv
else:
    import unicodecsv as csv

@DenisCarriere
Copy link
Author

DenisCarriere commented Mar 26, 2015 via email

@jdunck
Copy link
Owner

jdunck commented Apr 15, 2015

I've added py3 in 0.12.0 - please try it out and open another issue if you find specific problems. :)

@fletchowns
Copy link

@jdunck Thank you! It seems to work great for me. The only thing I noticed was that the setup.py has not been updated to indicate that this package works with Python 3. Created #55 for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants