从python输出中删除字符

时间:2015-11-30 12:19:03

标签: python apache-spark

我做了大量的工作来删除spark python输出中的字符,如 u u' U" [()/'" 正在为我做进一步的工作带来问题。所以请关注同样的事情。

我有像

这样的输入
(u"(u'[25145,   12345678'", 0.0)
(u"(u'[25146,   25487963'", 43.0) when i applied code to summing out the result. this gives me the output like
(u'(u"(u\'[54879,    5125478\'"', 0.0)
(u"(u'[25145,   25145879'", 11.0)
(u'(u"(u\'[56897,    22548793\'"', 0.0) so i want to remove all the character like (u'(u"(u\'["'') 

我想输出

54879,5125478,0.0

25145,25145879,11.0

我试过的代码是

from pyspark import SparkContext
import os
import sys

sc = SparkContext("local", "aggregate")

file1 = sc.textFile("hdfs://localhost:9000/data/first/part-00000")
file2 = sc.textFile("hdfs://localhost:9000/data/second/part-00000")

file3 = file1.union(file2).coalesce(1).map(lambda line: line.split(','))

result = file3.map(lambda x: ((x[0]+', '+x[1],float(x[2][:-1])))).reduceByKey(lambda a,b:a+b).coalesce(1)

result.saveAsTextFile("hdfs://localhost:9000/Test1")

1 个答案:

答案 0 :(得分:1)

我认为您唯一的问题是在将结果保存到文件之前必须重新格式化结果,例如:

result.map(lambda x:x[0]+','+str(x[1])).saveAsTextFile("hdfs://localhost:9000/Test1")
相关问题