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

csvstat: UnicodeError in Python 2 when --csv option enabled #944

Closed
binarytooth opened this issue Mar 18, 2018 · 4 comments
Closed

csvstat: UnicodeError in Python 2 when --csv option enabled #944

binarytooth opened this issue Mar 18, 2018 · 4 comments
Labels

Comments

@binarytooth
Copy link

csvstat 1.0.2
Python 2.7.12
Ubuntu 16.04.3 LTS xenial

When I run csvstat on a file containing a header and a single euro sign € (ISO-8859-15 character A4), it exits with an error, but only if the --csv option is included. If the option is not included csvstat generates output correctly. A copy of the test file

euro-sign-iso-8859-15.txt

is attached to this ticket.

This happens with all characters from 0x80 (128) through 0xFF (255) with the exception of 0x85 (Next line character) All produce the same error when csvstat is run with the --csv option

When csvstat is run on the test file without the csv option, it produces the following correct output:

$ csvstat -e ISO-8859-15 euro-sign-iso-8859-15.txt

  1. "Contents"
Type of data:          Text
Contains null values:  False
Unique values:         1
Longest value:         1 characters
Most common values:    € (1x)

Row count: 1

When I run the same command but with the --csv option enabled it blows up.

$ csvstat -v -e ISO-8859-15 euro-sign-iso-8859-15.txt --csv
column_id,column_name,type,nulls,unique,min,max,sum,mean,median,stdev,len,freq
Traceback (most recent call last):
File "/usr/local/bin/csvstat", line 9, in
load_entry_point('csvkit==1.0.2', 'console_scripts', 'csvstat')()
File "/usr/local/lib/python2.7/dist-packages/csvkit/utilities/csvstat.py", line 335, in launch_new_instance
utility.run()
File "/usr/local/lib/python2.7/dist-packages/csvkit/cli.py", line 114, in run
self.main()
File "/usr/local/lib/python2.7/dist-packages/csvkit/utilities/csvstat.py", line 166, in main
self.print_csv(table, column_ids, stats)
File "/usr/local/lib/python2.7/dist-packages/csvkit/utilities/csvstat.py", line 318, in print_csv
writer.writerow(output_row)
File "/usr/local/lib/python2.7/dist-packages/agate/csv_py2.py", line 190, in writerow
UnicodeWriter.writerow(self, row)
File "/usr/local/lib/python2.7/dist-packages/agate/csv_py2.py", line 103, in writerow
self.writer.writerow([six.text_type(s if s is not None else '').encode(self.encoding) for s in row])
File "/usr/lib/python2.7/codecs.py", line 369, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 32: ordinal not in range(128)

This problem also occurs for files encoded using WINDOWS-1252. This bug prevents the use of many useful characters in csv files such as smart quotes, dagger, double dagger, many accented letters, the euro sign, etc.

@jpmckinney jpmckinney added the bug label Mar 26, 2018
@jpmckinney
Copy link
Member

It's an issue in Python 2 only. I tried to identify the issue, but running the same code in a Python shell doesn't reproduce the error – the error only occurs when run from the command-line.

@jpmckinney jpmckinney changed the title csvstat blows up on all high ascii characters when --csv option is enabled csvstat: UnicodeError in Python 2 when --csv option enabled Mar 26, 2018
@aborruso
Copy link

aborruso commented Apr 1, 2018

The same for me.

You have this error with python 2.7 when you have accented characters.

Try with console and this csv file

fieldA,fieldB
aa,bb
aa,cc
hello,world
à,b

@TomekZet
Copy link

TomekZet commented May 11, 2018

I get similar problem with csvlook, only when redirecting output to a file:

$ csvlook -
fieldA,fieldB
aa,bb
aa,cc
hello,world
à,b
| fieldA | fieldB |
| ------ | ------ |
| aa     | bb     |
| aa     | cc     |
| hello  | world  |
| à      | b      |

When printing directly to the console it is ok, because I have UTF-8 locale set:

$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
[...]
$ csvlook - > test.tmp
fieldA,fieldB
aa,bb
aa,cc
hello,world
à,b
'ascii' codec can't encode character u'\xe0' in position 2: ordinal not in range(128)

Looks as if you don't encode unicode output properly.
I am not sure but probably the missing encode should be addede to https://github.com/wireservice/agate/blob/master/agate/table/print_table.py

The problem is not present in versions <= 1.0.0
The earliest version in which I can reproduce it is 1.0.1

@jpmckinney
Copy link
Member

Closing as Python 2 no longer supported.

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

No branches or pull requests

4 participants