简体   繁体   English

在Python中处理标准输入并重定向到标准输出

[英]Manipulating stdin and redirect to stdout in Python

I'm trying to write a simple python script where 我正在尝试编写一个简单的python脚本,其中

  1. it takes values from stdin 它从标准输入取值
  2. replaces a specific matched word 替换特定的匹配词
  3. passes on the output with the NEW value back to stdout 将具有新值的输出传递回stdout

I only have the part where it takes the values from stdin and looks for the matching words, I'm a bit stuck after that. 我只有一部分从stdin中获取值并寻找匹配的单词,在那之后我有点卡住了。

import re
import sys

for line in sys.stdin:
    matchObj = re.search(r'<something>(.*)</something>',line)
    if matchObj:
        oldWord = matchObj.group(1)
        print oldWord

Contents of foo foo的内容

<something>REPLACEME</something>
<blah>UNTOUCH</blah>

Ideally if I run this command 理想情况下,如果我运行此命令

cat foo | ./test.py

I would get something like this 我会得到这样的东西

<something>NEWWORD</something
<blah>UNTOUCH</blah>

Are you looking for re.sub ? 您在寻找re.sub吗?

import re
import sys

for line in sys.stdin:
    sys.stdout.write(re.sub(r'(<something>)REPLACEME(</something>)',
                            r'\1NEWWORD\2',
                            line))

Running the above on your example data: 在示例数据上运行以上代码:

$ echo '<something>REPLACEME</something>\n<something>UNTOUCH</something>' | python2 test.py
<something>NEWWORD</something>
<blah>UNTOUCH</blah>

Note that parsing XML with regular expressions is probably a bad idea. 请注意,使用正则表达式解析XML可能不是一个好主意。 The Python standard library comes with a number of XML modules . Python标准库带有许多XML模块

Here's an example: 这是一个例子:

import sys
import xml.etree.ElementTree

tree = xml.etree.ElementTree.parse(sys.stdin)
root = tree.getroot()

for node in root.iter('something'):
    if node.text == 'REPLACEME':
        node.text == 'NEWWORD'

tree.write(sys.stdout)

The above would work just the same: 上面的工作原理是一样的:

$ echo '<root><something>REPLACEME</something>\n<blah>UNTOUCH</blah></root>' | python2 test.py
<root><something>REPLACEME</something>
<blah>UNTOUCH</blah></root>

firs if you run cat foo | ./test.py 如果您运行cat foo | ./test.py则将cat foo | ./test.py cat foo | ./test.py you got test.py: command not found , you need to run this : cat foo |python ./test.py . cat foo | ./test.pytest.py: command not found ,您需要运行以下命令: cat foo |python ./test.py

then the output of your code will be : 那么您的代码输出将是:

REPLACEME

but for the output that you want, you need to use re.sub() : 但是对于所需的输出,您需要使用re.sub()

import re
import sys

for line in sys.stdin:
    matchObj = re.sub(r'<something>(.*)</something>','<something>NEWWORD</something>',line)
    if matchObj:
        print matchObj

output : 输出:

<something>NEWWORD</something>

<blah>UNTOUCH</blah>

Also as a pythonic way you can use The ElementTree XML API 另外,作为Python方式,您可以使用ElementTree XML API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM