1

I am trying to port a module from python2.7 to python3.4. In python2.7 (code below) the classifier takes a string object (i) and returns output in a matter of milliseconds read from stdout.

However when I change the code to python3.4 stdin complains it only takes bytes input. Converting the input (i) to bytes using

i = i.encode('utf-8)

works but no output can be read from stdout. Why?

Unfortunately I am not familiar with the code in tweets.annotated.csv.model

class CapClassifier:
    def __init__(self, model='%s/data/cap2/tweets.annotated.csv.model' % (BASE_DIR)):
        self.capClassifier = subprocess.Popen('%s/python/cap/cap_classify %s/data/cap2/tweets.annotated.csv.model' % (BASE_DIR, BASE_DIR),
                                          shell=True,
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE)
        self.fe = FeatureExtractor('%s/data/cap2/tweets_cap.vocab' % (BASE_DIR))

    def Classify(self, words):
        i = "%s\n" % self.fe.Extract(' '.join(words))
        self.capClassifier.stdin.write(i)
        (features, prediction) = self.capClassifier.stdout.readline().rstrip('\n').split('\t')
        return float(prediction)

2 Answers 2

1

Turns out I had to add universal_newlines=True to accept strings as stated in Robin's answer and add self.capClassifier.stdin.flush() after the write-statement for results to return

Sign up to request clarification or add additional context in comments.

Comments

0

Popen.stdin

If the stdin argument was PIPE, this attribute is a writeable stream object as returned by open(). If the universal_newlines argument was True, the stream is a text stream, otherwise it is a byte stream. If the stdin argument was not PIPE, this attribute is None.

May this quote from the python documentation helps. Add universal_newlines=True to the subprocess.Popen constructor.

1 Comment

The error (TypeError: a bytes-like object is required, not 'str') is now gone, however I still do not obtain output from stdout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.