python - Data Appending issue -
i trying add combination of words in existing string using python. achieve have written below code.
import subprocess subprocess import popen, pipe cat = subprocess.popen(["hadoop", "fs", "-cat", "/user/cloudera/rank_t/*"], stdout=subprocess.pipe) dumpoff = popen(["hadoop", "fs", "-put", "-", "/user/cloudera/data"],stdin=pipe) obrind = "0" line1 = "" line in cat.stdout: runnno= line.split('|')[0] code = line.split('|')[1] idval = line.split('|')[2] if (code == "obr"): obrind = runnno line =line + "|"+"obr_"+obrind dumpoff.stdin.write(line) print(line)
my sample data:
1|orc||4002c3|4002c3||||||20141231|||1962 2|obr|1||4002c3|197 hp, rx 16/l|||20141|20141||||||||196248||rj||3711028|||||f 3|obx|1|st|2263||negative intraepithelial l.||||||f|||20141231|rj @#l 4|nte|1|l|negative intraepithelial lesion , . 5|obx|2|st|1158||nil||||||f|||20141231|rj@#l
expected output:
1|orc||4002c3|4002c3||||||20141231|||1962| 2|obr|1||4002c3|197 hp, rx 16/l|||20141|20141||||||||196248||rj||3711028|||||f|obr_1 3|obx|1|st|2263||negative intraepithelial l.||||||f|||20141231|rj @#l|obr_1 4|nte|1|l|negative intraepithelial lesion , .|obr_1 5|obx|2|st|1158||nil||||||f|||20141231|rj@#l|obr_1
actual output:
1|orc||4002c3|4002c3||||||20141231|||1962| 2|obr|1||4002c3|197 hp, rx 16/l|||20141|20141||||||||196248||rj||3711028|||||f |obr_1 3|obx|1|st|2263||negative intraepithelial l.||||||f|||20141231|rj @#l |obr_1 4|nte|1|l|negative intraepithelial lesion , . |obr_1 5|obx|2|st|1158||nil||||||f|||20141231|rj@#l |obr_1
the word trying append appending in new line want appending in same line. doing wrong ?
that's because each line
has \n
@ end. can strip string .strip()
:
line = line.strip() + "|"+"obr_"+obrind
or
line = line.strip('\n') + "|"+"obr_"+obrind
if care white spaces @ begining/end of line.
Comments
Post a Comment