首页 > 解决方案 > GCP 中的 PySpark 内核 - Unicode 字符串

问题描述

我有一个包含字符串的列的数据框。当我调用函数时:

df = spark.read.csv(path, header=True).show()

我得到正确的“视图”,但是当我打印时

print("dataframe as a RDD object (list of Row objects):\n\t", df.collect())

结果是带有 unicode 符号的字符串,例如u'mystring'

我怎样才能解决这个问题

标签: pythongoogle-cloud-platformpyspark

解决方案


在 Python 2.x 中,您拥有strunicode. 周围的对象u'mytext'是 Unicode。

要将 unicode 转换为 str:

mystr = unistr.encode('utf-8')

要将 str 转换为 unicode:

unistr = mystr.decode('utf-8')

在 Python 2.x 中,我通常将字符串保留为 Unicode,直到需要将它们写入文件等。在 Python 3.x 中,所有字符串都是 Unicode。

以下文档将有助于理解:

统一码如何


推荐阅读