spark 报错:UnicodeEncodeError: ‘ascii‘ codec can‘t encode characters in position 解决方法

pyspark 调用DataFrame.show()进行数据展示,展示的数据存在中文,会报UnicodeEncodeError: 'ascii' codec can't encode characters in position 284-285: ordinal not in range(128)异常。

.py文件中已指定#coding:utf-8编码,需要更改系统默认编码格式:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

异常原文如下:

Traceback (most recent call last):
  File "Demo5.py", line 33, in <module>
    read_df.show()
  File "/opt/cloudera/parcels/CDH-5.16.2-1.cdh5.16.2.p0.8/lib/spark/python/lib/pyspark.zip/pyspark/sql/dataframe.py", line 257, in show
UnicodeEncodeError: 'ascii' codec can't encode characters in position 284-285: ordinal not in range(128)

posted on 2020-07-31 14:35  疯狂的小萝卜头  阅读(1998)  评论(0编辑  收藏  举报