使用pygal在asthon中渲染文件 - ascii代码错误

时间:2014-11-03 19:56:30

标签: python python-2.7 python-2.6 pygal

我正在尝试在python中创建pygal图表并将其保存为.svg文件。

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

这段简单的代码似乎给出了错误 -

> Traceback (most recent call last):   File
> "/home/ec2-user/githubr/migrationcharts.py", line 161, 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)

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

尝试将您发送给unicode的路径字符串解码为render_to_file。 如:

pie_chart.render_to_file('path/to/overallProgress.svg'.decode('utf-8'))

解码字符集应与您的文件编码一致。