【问题标题】:rendering a file in python using pygal - ascii code error使用pygal在python中渲染文件-ascii代码错误
【发布时间】:2014-11-03 19:56:29
【问题描述】:

我正在尝试在 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)

知道为什么吗?

【问题讨论】:

    标签: python python-2.7 python-2.6 pygal


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2023-03-25
      • 2022-10-16
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多