在 python 中使用管道
import re
import subprocess
sub = subprocess.Popen(['/home/karthik/Downloads/stanford-parser-2011-06- 08/lexparser.csh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
sub.stdin.write("i am a fan of ac milan which is the best club in the world")
relns = []
while(True):
rel = sub.stdout.readline()
m = re.search("Sentence skipped", rel)
if m != None:
print 'stop'
sys.exit(0)
if rel == '\n':
break
relns.append(rel)
print relns
sub.terminate()
所以我想要斯坦福解析器并使用 lexparser.csh 来解析这行文本。但是当我运行这段代码时,我得到了默认文本的输出。给出的实际文本没有被解析。那么我是否以正确的方式使用管道?我在很多例子中看到过 - '-' 与命令一起使用。为什么要使用它?因为当我使用该脚本时,脚本只是停在 sub.stdout.readline() 处
import re
import subprocess
sub = subprocess.Popen(['/home/karthik/Downloads/stanford-parser-2011-06- 08/lexparser.csh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
sub.stdin.write("i am a fan of ac milan which is the best club in the world")
relns = []
while(True):
rel = sub.stdout.readline()
m = re.search("Sentence skipped", rel)
if m != None:
print 'stop'
sys.exit(0)
if rel == '\n':
break
relns.append(rel)
print relns
sub.terminate()
So i want to the stanford parser and using the lexparser.csh to parse this line of text . But when i run this piece of code i am a getting the output of the default of text. The actual text given in is not being parsed. So am i using pipes the right way ?And i've seen in a lot of examples - a '-' is used along with the command . Why is that being used ? Cos when i use that the script just stalls at sub.stdout.readline()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
写入后,您可能需要在
sub.stdin
上调用flush()
。You may need to call
flush()
onsub.stdin
after writing.