python-2.7 - 读取数据时如何删除unicode?

标签 python-2.7 unicode utf-8 apache-spark pyspark

我有以下 Python 代码行:

trans = data.map(lambda line: line.strip().split())

产生 Unicode 字符串,例如:
u'Hello',u'word'

我想获得普通的 UTF-8 或 ASCII 字符串
'Hello','word' 

我尝试将字符串转换为 UTF-8,例如
trans = data.map(lambda line: line.strip().split().encode("utf-8"))

或者
trans = data.map(lambda line: line.strip().split().encode('ascii','ignore'))

但这给出了一个错误:
AttributeError: 'list' object has no attribute 'encode'

谁能告诉我我怎么能做到这一点?

更新:

数据是scv文件,
反式是RDD

最佳答案

为什么不简单地编码和拆分:

data = sc.textFile("README.md")
trans = data.map(lambda x: x.encode("ascii", "ignore").split())
trans.first()
## ['#', 'Apache', 'Spark']

关于python-2.7 - 读取数据时如何删除unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479444/

相关文章:

python-2.7 - 选择 Python 后立即选择选项菜单

python - 将数组分组为元组数组 - 奇数长度

python - 在 Python 中动态创建列表和字典。我正在尝试用 Python 创建数组

java - 以 Base64 和 utf-8 解码电子邮件主题字符串

换行后 Unicode 字符变得困惑

python - 理解使用生成器的函数中的递归

MySQL UTF 文本容量

c# - 阿拉伯语问题 将 أً 替换为 ا

python - 从句子字符串中剥离所有表情符号

mysql - 文件编码(UTF-8 无法正常工作)