栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

cat,grep和cut-翻译成python

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cat,grep和cut-翻译成python

您需要更好地了解python语言及其标准库才能翻译表达式

cat“ $ filename”:读取文件

cat"$filename"
并将内容转储到stdout

|
:管道将上
stdout
一条命令重定向,并将其馈送到
stdin
下一条命令的

grep“ something”:搜索正则表达式

something
纯文本数据文件(如果已指定)或在stdin中,并返回匹配的行。

cut -d’“’-f2:使用特定的定界符分割字符串,并从结果列表中索引/拼接特定字段

相当于Python

cat "$filename"  | with open("$filename",'r') as fin:        | Read the file Sequentially      |     for line in fin:|   -----------------------------------------------------------------------------------grep 'something' | import re| The python version returns      | line = re.findall(r'something', line)[0]  | a list of matches. We are only      |          | interested in the zero group-----------------------------------------------------------------------------------cut -d'"' -f2    | line = line.split('"')[1]      | Splits the string and selects      |          | the second field (which is      |          | index 1 in python)

结合

import rewith open("filename") as origin_file:    for line in origin_file:        line = re.findall(r'something', line)        if line:line = line[0].split('"')[1]        print line


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412594.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号