Installing unicode csv for Python?
The problem is that you're using Python 3.0 or later, and trying to use a library which is only compatible with 2.7 and earlier.
The specific problem is the line the traceback points at:
except TypeError, e:
This syntax was deprecated in 2.6, in favor of (more flexible and more consistent) new syntax:
except TypeError as e:
In 3.0 and later, the deprecated syntax is no longer allowed at all.
So, if you want to use this library, someone will have to port it—you, the author, or someone else. It may just be a matter of running 2to3
, or fixing each except
statement manually—but it may be a lot more to do than that, especially considering this library is all about Unicode.
However, it's worth noting that Python 3.x doesn't have the same problem as 2.x did. You can pass the csv
module text/Unicode file objects, and it will just handle them. Adapting the example from the unicodecsv
docs:
>>> import csv
>>> from io import StringIO
>>> f = StringIO()
>>> w = csv.writer(f)
>>> w.writerow(('é', 'ñ'))
>>> f.seek(0)
>>> r = csv.reader(f)
>>> row = r.next()
>>> print row[0], row[1]
é ñ
Note that I didn't even have to specify utf-8
, because StringIO
is a Unicode str
buffer, not a bytes
buffer. You don't have to worry about coding at all.
If you didn't even know you were running Python 3.x (as in, you knew you installed it, but you were careful to keep Apple's pre-installed Python 2.7 higher on the PATH), there are three common reasons this can happen.
Apple's Python doesn't come with
pip
; all of the popular Python 3.x installers and packages do. So, if you haven't installedpip
for 2.7, the only one you have is 3.x.Apple's Python installs scripts like
pip
to/usr/local/bin
. So do some of the popular Python 3.x installers and packages. So, whichever you installed more recently wins. (The 3.x one should also be available as/usr/local/bin/pip3
, so overwriting its/usr/local/bin/pip
with 2.7's is usually fine… unlesspip3
is a symlink topip
.)sudo
deliberately discards most of your user environment, so 2.7 may be higher on yourPATH
when running as yourself, but not when running withsudo
.
![David Bailey](https://i.stack.imgur.com/3OjJ0.jpg?s=256&g=1)
Comments
-
David Bailey about 2 years
I'm new to github and I'm trying to install unicodecsv (https://github.com/jdunck/python-unicodecsv).
I'm trying
sudo pip install -e git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
But I'm getting an error message. I'm probably doing someone basic wrong, can someone help?
Obtaining unicodecsv from git+git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv Cloning git://github.com/jdunck/python-unicodecsv.git to ./src/unicodecsv Running setup.py egg_info for package unicodecsv Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module> version = __import__('unicodecsv').__version__ File "unicodecsv/__init__.py", line 49 except TypeError, e: ^ SyntaxError: invalid syntax Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module> version = __import__('unicodecsv').__version__ File "unicodecsv/__init__.py", line 49 except TypeError, e: ^ SyntaxError: invalid syntax ---------------------------------------- Command python setup.py egg_info failed with error code 1 Storing complete log in /Users/dave/.pip/pip.log Davids-MacBook-Air:working dave$ sudo pip install -e git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv Obtaining unicodecsv from git+git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv Updating ./src/unicodecsv clone ^[ Running setup.py egg_info for package unicodecsv Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module> version = __import__('unicodecsv').__version__ File "unicodecsv/__init__.py", line 49 except TypeError, e: ^ SyntaxError: invalid syntax Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 14, in <module> File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module> version = __import__('unicodecsv').__version__ File "unicodecsv/__init__.py", line 49 except TypeError, e: ^ SyntaxError: invalid syntax ---------------------------------------- Command python setup.py egg_info failed with error code 1 Storing complete log in /Users/dave/.pip/pip.log
-
Ignacio Vazquez-Abrams about 11 yearsThat's not what the new syntax looks like.
-
abarnert about 11 years@IgnacioVazquez-Abrams: Oops, I originally wrote about the new
raise
andexcept
syntax, then decided that was more detail than necessary and removed theraise
parts, then edited it incorrectly. Thanks for the catch! -
David Bailey about 11 yearsThanks very much for this. The issue I have is that I am reading data from the CSV with ? replacing special characters. For example, "São Paulo" reads as "S?o Paulo".
-
abarnert about 11 years@DavidBailey: Yes, in Python 2.7 the module is useful; that's why I said "Python 3.x doesn't have the same problem as 2.x did". Most likely, your
python
is actually/usr/bin/python
, which is 2.7.2, while yourpip
is/usr/local/bin/pip
or/Library/Frameworks/Python.framework/Versions/3.3/bin/pip
is something, which is 3.3, which is going to confuse you. That's why I wrote a whole section on this in the answer, which you should read. -
David Bailey about 11 yearsSorry, reread and installed pip for 2.7, now have safely installed. Thanks and my bad.