Jens de Bruijn
Jens de Bruijn

Reputation: 969

stdin takes string in python2.7 but only takes bytes in python3.4

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)

Upvotes: 1

Views: 438

Answers (2)

Jens de Bruijn
Jens de Bruijn

Reputation: 969

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

Upvotes: 1

ITiger
ITiger

Reputation: 1081

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.

Upvotes: 0

Related Questions