【发布时间】:2016-08-12 23:18:51
【问题描述】:
我需要对一个巨大的文本文件中的单词进行计数,但在此之前,我必须以特定的方式清理特殊字符的文件。
例如——
;xyz --> xyz
xyz: --> xyz
xyz!) --> xyz!
我正在使用 flatMap() 来分割空间上的所有单词。然后我试图删除不起作用的特殊字符。请帮忙!
这是我正在使用的代码 ---
要删除的字符是 - : ; ! ? ( ) .
>>> input = sc.textFile("file:///home/<...>/Downloads/file.txt")
>>> input2 = input.flatMap(lambda x: x.split())
>>> def remove(x):
if x.endsWith(':'):
x.replace(':','')
return x
elif x.endsWith('.'):
x.replace('.','')
return x
。 .
>>> input3 = input2.map(lambda x: remove(x))
【问题讨论】:
-
为什么它不起作用?请发布您尝试过的内容。
-
在第三个例子中“!”字符不是特殊字符?
-
你能定义什么是特殊字符吗?
-
要删除的字符是 - : ; ! ? ( ) 。 input = sc.textFile("file:///home/<...>/Downloads/file.txt") >>> input2 = input.flatMap(lambda x: x.split()) >>> def remove (x): if x.endsWith(':')==true: x.replace(':','') return x elif x.endsWith('.')==true: x.replace('.' ,'') 返回 x 。 . >>> input3 = input2.filter(lambda x: remove(x))
标签: python regex special-characters pyspark word-count