Commit 9621b8a5b76fd8bbb03b274c3aecd563299b3e3e

Authored by Antoine Goutenoir
1 parent e11f438a
Exists in master

Fix the CSV generation choking on unicode.

Showing 1 changed file with 14 additions and 3 deletions   Show diff stats
flaskr/controllers/main_controller.py
... ... @@ -21,6 +21,9 @@ from yaml import safe_dump as yaml_dump
21 21 main = Blueprint('main', __name__)
22 22  
23 23  
  24 +OUT_ENCODING = 'utf-8'
  25 +
  26 +
24 27 @main.route('/favicon.ico')
25 28 def favicon(): # we want it served from the root, not from static/
26 29 return send_from_directory(
... ... @@ -397,15 +400,23 @@ def consult_estimation(public_id, format):
397 400  
398 401 si = StringIO()
399 402 cw = csv.writer(si, quoting=csv.QUOTE_ALL)
400   - cw.writerow([u"city", u"co2 (g)"])
  403 + cw.writerow([u"city", u"address", u"co2 (g)"])
401 404  
402 405 results = estimation.get_output_dict()
403 406 if 'mean_footprint' in results:
404 407 for city in results['mean_footprint']['cities']:
405   - cw.writerow([city['city'], city['address'], city['footprint']])
  408 + cw.writerow([
  409 + city['city'].encode(OUT_ENCODING),
  410 + city['address'].encode(OUT_ENCODING),
  411 + city['footprint']
  412 + ])
406 413 elif 'cities' in results:
407 414 for city in results['cities']:
408   - cw.writerow([city['city'], city['address'], city['total']])
  415 + cw.writerow([
  416 + city['city'].encode(OUT_ENCODING),
  417 + city['address'].encode(OUT_ENCODING),
  418 + city['total']
  419 + ])
409 420  
410 421 # HTTP headers?
411 422 return si.getvalue().strip('\r\n')
... ...