1

Trying to strip characters out of output from the following snippit: subprocess.Popen(cmd,shell=true,stdout=subprocess.PIPE)

Longterm i need to split the output into seperate list indexes. Trying to test this with stdout output to validate that its working first. Here is a snippit of the code right now:

#!/usr/bin/env python3.4

import subprocess

def writefile():
  sites = ['blue.com', 'red.com']               
  for site in sites:
    proc = subprocess.Popen(["python dnstwist.py -g " + site + "| sed -e '1,8d' | grep -v -"],shell=True,stdout=subprocess.PIPE,universal_newlines=True)
    while True:
      line = proc.stdout.readline()
      if line!= '':
        print(line.strip('/'))
      else: 
        break

I have also tried:

      line = proc.stdout.readline().strip('/')
      if line!= '':
        print(line)
      else: 
        break

output always looks like this sample. Notice the forwardslash is not being stripped out:

Replacement     4ed.com    212.48.70.37/United Kingdom MX:mail.4ed.com

Replacement     r4d.com    69.172.201.208/United States

Also it looks like its adding \n or \r.

Longterm i would like it to be something i can parse out to look like the following:

{'4ed.com': ['Replacement', '212.48.70.37', 'somewhoisdata', 'United Kingdom'] }

...etc

dobbs
  • 899
  • 4
  • 17
  • 36
  • try using `strip()` not `rstrip()` – coneyhelixlake Oct 05 '15 at 19:02
  • 1
    note that rstrip only takes away the characters in your argument (in this case, "/") if it is trailing, not all the occurrences of the character in the string – R Nar Oct 05 '15 at 19:03
  • and strip is for both trailing and leading – R Nar Oct 05 '15 at 19:04
  • Just tried strip and had the same results. the output still contains "/" while True: line = proc.stdout.readline() if line!= '': print(line.strip('/')) – dobbs Oct 05 '15 at 19:05
  • 1
    use `replace('/', '')`. – Bakuriu Oct 05 '15 at 19:14
  • 1
    as suggested by @RNar, ff the replacement text you've included is correct you should be using `replace('/', '')` instead of `strip()`. If you want to replace only the last occurence of `/` then try what is suggested [here](http://stackoverflow.com/questions/14496006/finding-last-occurence-of-substring-in-string-replacing-that) – ziddarth Oct 05 '15 at 19:16
  • you don't need `shell=True` here: `sed` and `grep` parts could be done in Python easily. Here's how to [read output from a subprocess line by line](http://stackoverflow.com/a/17698359/4279). Consider [importing the python module and calling the corresponding function (perhaps using `multiprocessing`) instead of spawning a subprocess and processing its output](http://stackoverflow.com/q/30076185/4279) – jfs Oct 06 '15 at 04:55

0 Answers0