简体   繁体   English

从 python 输出中删除字符

[英]Removing Characters from python Output

I did alot of work to remove the characters from the spark python output like uu' u" [()/'" which are creating problem for me to do the further work.我做了很多工作来从 spark python 输出中删除字符,比如uu' u" [()/'" ,这给我做进一步的工作带来了问题。 So please put a focus on the same .所以请把重点放在相同的地方。

I have the input like,我有这样的输入,

(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\'["'') 

I want output like我想要输出像

54879,5125478,0.0

25145,25145879,11.0

the code is i tried is我试过的代码是

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")

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM