【问题标题】:Same python script works in one machine, gives error in the second one - UnicodeDecodeError相同的 python 脚本在一台机器上工作,在第二台机器上出错 - UnicodeDecodeError
【发布时间】:2015-01-01 22:06:05
【问题描述】:

我在 python 中使用 pygal 创建图表,脚本在一台机器上运行良好,但在第二台机器上产生 unicode 解码错误。不知道为什么。

#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")

一次工作,但第二次出错:

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)

运行 pdb 并得到了这个:

Traceback(最近一次调用最后一次):

  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

【问题讨论】:

  • 您确定在两个终端上使用相同的编码吗?
  • UTF - 8。一个是windows命令提示符,另一个是linux终端。
  • 第一次听说cmd.exe支持utf-8作为终端编码。
  • oops sorry utf-8 in linux 终端(失败)并且在 windows cmd 中运行流畅。

标签: python python-2.7 python-2.6 python-2.x


【解决方案1】:

试试:

pie_chart.title = unicode('Github/Jenkins-Migration Status Chart (in %)', "utf-8")

【讨论】:

  • 如果我制作了 pie_chart.render(),它会成功,但由于 unicode 错误而无法渲染到文件。
【解决方案2】:
>>> import sys
>>> sys.getdefaultencoding() 
ascii
>>> sys.setdefaultencoding('utf8')
>>> sys.getdefaultencoding()
utf8

我就是这样解决的。

【讨论】:

  • 解释器启动时,库存site 模块从sys 中删除setdefaultencoding
  • 在我的脚本中添加了这个,以便在脚本运行之前每次都设置它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
相关资源
最近更新 更多