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

UnicodeEncodeError: 'ascii' codec can't encode character #32

Open
eraclitux opened this issue May 2, 2014 · 1 comment
Open

UnicodeEncodeError: 'ascii' codec can't encode character #32

eraclitux opened this issue May 2, 2014 · 1 comment

Comments

@eraclitux
Copy link

Hi, got this:

Traceback (most recent call last):
  File "/opt/easycloud_django/easypanel/views.py", line 882, in adminServerReportCsv
    report_server.generate_by(CSVGenerator, filename=resp)
  File "/opt/easycloud_env/lib/python2.6/site-packages/geraldo/base.py", line 435, in generate_by
    return generator.execute()
  File "/opt/easycloud_env/lib/python2.6/site-packages/geraldo/generators/csvgen.py", line 66, in execute
    self.generate_csv()
  File "/opt/easycloud_env/lib/python2.6/site-packages/geraldo/generators/csvgen.py", line 119, in generate_csv
    self.writer.writerow(cells)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 3: ordinal not in range(128)

When subclassing:

class myBandDetail(DetailBand):
                elements = [
                    ObjectValue(expression='user.username'),
                    ObjectValue(expression='user.first_name'),
                    ObjectValue(expression='user.last_name'),
                    ObjectValue(expression='name'),
                    ObjectValue(expression='plan.name'),
                    ObjectValue(attribute_name='creation_date',
                                get_value=lambda instance: instance.creation_date.strftime('%d/%m/%Y %H:%M')),
                    ObjectValue(attribute_name='deletion_date',
                                get_value=lambda instance: instance.deletion_date.strftime('%d/%m/%Y %H:%M') if instance.deletion_date else '',
                    ),
                    ObjectValue(expression='month_billing',
                                get_value=lambda instance: ('%.2f' % instance.month_billing).replace('.',','),
                    ),
                ]

where user.last_name contains "è" or other non-ascii char

@fvlima
Copy link

fvlima commented Mar 18, 2016

I had the same problem, and I have created my own CSVGenerator inheriting from the orginal, and overriding the method generate_csv to force self.writer.writerow([cell.encode('utf-8') for cell in cells]). This solved my problem.

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

2 participants