Pyspark如何去除标点符号并在Rdd中制作小写字母?

Pyspark how to remove punctuation marks and make lowercase letters in Rdd?

我想把RDD中的标点符号去掉,变成小写字母? 下面是我的数据集

 l=sc.parallelize(["How are you","Hello\ then% you"\
,"I think he's fine+ COMING"])

我尝试了以下功能,但收到错误消息

punc='!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'

def lower_clean_str(x):
    lowercased_str = x.lower()
    clean_str = lowercased_str.translate(punc) 
    return clean_str

one_RDD = l.flatMap(lambda x: lower_clean_str(x).split())
one_RDD.collect()

但这给了我一个错误。可能是什么问题?我怎样才能解决这个问题? 谢谢。

您使用的 python 翻译功能有误。 由于我不确定您使用的是 python 2.7 还是 python 3,因此我建议采用另一种方法。

翻译功能在 python 3.

中发生了一些变化

无论 python 版本如何,以下代码都可以工作。

def lower_clean_str(x):
  punc='!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
  lowercased_str = x.lower()
  for ch in punc:
    lowercased_str = lowercased_str.replace(ch, '')
  return lowercased_str

l=sc.parallelize(["How are you","Hello\ then% you","I think he's fine+ COMING"])
one_RDD = l.map(lower_clean_str)
one_RDD.collect()

输出:

['how are you', 'hello then you', 'i think hes fine coming']