python - 如何在标准输入的 python 条件下也只读取一行
问题描述
我将以下数据传递给 stdin stdout :
aa
a
aaa
aaaaa
[new,aaa] < name of the file with path ]
orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap
[mod] <name of the file with path
orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap
我只想复制带有 python 路径的文件名。
if new keyword exists in stdin:
take the filename with path
else
nothing.
解决方案
您从标准输入读取,而不是标准输出。如果您将另一个程序的输出通过管道传输到 Python 脚本,它将位于 Python 的标准输入中。
您可以使用正则表达式来提取文件名。
import re
import sys
regex = re.compile(r'\[[^]]*new.*?\].*<\s*(.*?)\s*\]')
for line in sys.stdin:
m = regex.search(line)
if m:
filename = m.group(1)
break
推荐阅读
- ruby-on-rails - 安装 SASSC gem 时出现 AWS Codebuild 错误 ruby 版本错误
- amazon-web-services - 仅从 Lambda 访问 AWS EC2
- google-apps-script - How do I control output cell location in a google sheet using google script?
- algorithm - Floyd-Warshall algorithm on a directed graph in which every edge's length is either -1, 0, or 1
- c++ - narrow conversion compile warning. How to fix it?
- javascript - 使用 JQuery 循环遍历 DOM 元素以分配单击处理程序
- c# - How to prevent my Include() statement from populating the included entity's collection?
- python-3.x - 将字典列表分组为基于多个键的字典列表列表
- spring-boot - 使用 Docker Swarm 的 Spring Boot Admin 无法提取健康指标
- xml - 如何设置WADL文件的官方格式?