2016-04-29 3 views
1

モジュールをpython2.7からpython3.4に移植しようとしています。 Python2.7(以下のコード)では、分類子は文字列object(i)を取り、stdoutから読み込んだミリ秒単位で出力を返します。stdinはpython2.7で文字列を取りますが、python3.4でバイトを取るだけです。

しかし、コードをpython3.4に変更すると、stdinはバイトを入力するだけです。

を使用して入力(i)をバイトに変換しますが、出力はstdoutから読み取ることはできません。どうして?

残念ながら私は、Pythonドキュメントからのこの引用ができます可能性があり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) 

答えて

1

例外TypeError(私はロビンの答えで述べたように、文字列を受け入れるようにuniversal_newlines=Trueを追加して、結果が

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.

のコードに精通していないです。 subprocess.Popenコンストラクタにuniversal_newlines=Trueを追加します。

+0

エラーを返すようにするためにwrite -statement後self.capClassifier.stdin.flush()を追加する必要がありましたが判明:バイトのようなオブジェクトが必要な、ではない「STR ')は現在消えていますが、私はまだstdoutからの出力を得ていません –

関連する問題