0

I'm creating charts using pygal in python and the script works fine in one machine but produces unicode decode error in the second one. No idea why.

#TCreating pygal charts
pie_chart = pygal.Pie(style=DarkSolarizedStyle, legend_box_size = 20)
pie_chart.title = 'Github/Jenkins-Migration Status Chart (in %)'
pie_chart.add('Intro', int(intro))
pie_chart.add('In Progress', int(in_progress) )
pie_chart.add('Parallel', int(parallel))
pie_chart.add('ParallelBuild', int(parallelBuild))
pie_chart.add('Complete', int(complete))
pie_chart.render_to_file("../../../../../usr/share/nginx/html/TeamFornax/githubMigration/OverallProgress/overallProgress.svg")

Works in one but gives error in second :

Traceback (most recent call last):   File "migrationcharts.py", line 187, in <module>
    pie_chart.render_to_file("../../../../../usr/share/nginx/html/TeamFornax/githubMigration/OverallProgress/overallProgress.svg")
File "/usr/lib/python2.6/site-packages/pygal/ghost.py", line 149, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))   
File "/usr/lib/python2.6/site-packages/pygal/ghost.py", line 112, in render
    .render(is_unicode=is_unicode))   
File "/usr/lib/python2.6/site-packages/pygal/graph/base.py", line 293, in render
    is_unicode=is_unicode, pretty_print=self.pretty_print)   
File "/usr/lib/python2.6/site-packages/pygal/svg.py", line 271, in render
    self.root, **args)   
File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 1010, in tostring
    return string.join(data, "")   
File "/usr/lib64/python2.6/string.py", line 318, in join
    return sep.join(words) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 40: ordinal not in range(128)

Ran pdb and got this :

Traceback (most recent call last):

  File "/usr/lib64/python2.6/pdb.py", line 1296, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib64/python2.6/pdb.py", line 1215, in _runscript
    self.run(statement)
  File "/usr/lib64/python2.6/bdb.py", line 372, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "migrationcharts.py", line 185, in <module>
    pie_chart.render_to_file("../../../../../usr/share/nginx/html/TeamFornax/githubMigration/OverallProgress/overallProgress.svg")
  File "/usr/lib/python2.6/site-packages/pygal/ghost.py", line 149, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))
  File "/usr/lib/python2.6/site-packages/pygal/ghost.py", line 112, in render
    .render(is_unicode=is_unicode))
  File "/usr/lib/python2.6/site-packages/pygal/graph/base.py", line 293, in render
    is_unicode=is_unicode, pretty_print=self.pretty_print)
  File "/usr/lib/python2.6/site-packages/pygal/svg.py", line 271, in render
    self.root, **args)
  File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 1010, in tostring
    return string.join(data, "")
  File "/usr/lib64/python2.6/string.py", line 318, in join
    return sep.join(words)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 40: ordinal not in range(128)
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
Scooby
  • 3,371
  • 8
  • 44
  • 84

2 Answers2

0

Try:

pie_chart.title = unicode('Github/Jenkins-Migration Status Chart (in %)', "utf-8")
Andy
  • 49,085
  • 60
  • 166
  • 233
marsh
  • 2,592
  • 5
  • 29
  • 53
  • If I make pie_chart.render(), it succeeds, its failing on rendering to file for somereason with unicode error. – Scooby Nov 05 '14 at 19:37
0
>>> import sys
>>> sys.getdefaultencoding() 
ascii
>>> sys.setdefaultencoding('utf8')
>>> sys.getdefaultencoding()
utf8

Thats how I resolved it.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Scooby
  • 3,371
  • 8
  • 44
  • 84