我正在使用Python3来尝试模拟
awk '/STRING/'
我制作了一些可行的代码,但它在第一个实例上停止,而不是查找包含指定字符/字符串的所有行。
在我编写代码之后,我看到了这两个页面,但建议没有用
Print line containing "word" python
Search and get a line in Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
def cat(openfile): #Emulates cat#
with open(openfile) as file:
lines = file.readlines()
return ''.join(lines)
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
if CONTAINING in item:
return item.strip()
print(GETLINEWITH(cat('./Base.xaiml'), 'terminal'))
我有一个文件(./Base.xaiml),其中包含单词“terminal”的几个实例。我正在使用此文件和字符串进行测试。
编辑:我还想将结果保存到变量 -
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
def cat(openfile): #Emulates cat#
with open(openfile) as file:
lines = file.readlines()
return ''.join(lines)
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
if CONTAINING in item:
print(item.strip()) #I implemented the give suggestion
VAR = GETLINEWITH(cat('./Base.xaiml'), 'terminal'))
结果:
我试过Jon Clements的建议:
with open('./Base.xaiml') as fin:
matching = ('terminal' in line for line in fin)
for line in matching:
VAR = matching
print(VAR)
但是,输出是一个内存地址。 ''。join()和group()没有帮助。
EDIT2:
最新代码 问题 - 非类型错误和输出无法保存到变量
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
def cat(openfile): #Emulates cat#
with open(openfile) as file:
lines = file.readlines()
return ''.join(lines)
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
if CONTAINING in item:
print(item.strip())
for line in GETLINEWITH(cat('./Base.xaiml'), 'terminal'):
print(line)
答案 0 :(得分:3)
在这里,请注意,我只是为您明确地汇总了所有人的评论。
#!/usr/bin/env python3
def cat(openfile):
with open(openfile) as file:
return file.read()
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
if CONTAINING in item:
yield item.strip()
matchedlines = []
for line in GETLINEWITH(cat('./Base.xaiml'), 'terminal'):
print(line)
matchedlines.append(line)
print(matchedlines)
matchedlines
变量只是一种将变量匹配的行保存在变量中的方法。
另一个GETLINEWITH
实现是:
import re
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
m = re.search(CONTAINING, item)
if m:
yield m.string
可以让你匹配正则表达式。
答案 1 :(得分:1)
无论如何 - 它停止的原因"是因为你在比赛结束后返回....
def GETLINEWITH(FILECONTENTS, CONTAINING):
for item in FILECONTENTS.split('\n'):
if CONTAINING in item:
return item.strip() # <---- returns as soon as something's found
将其更改为yield
以使该函数成为生成器,然后循环它:
for line in GETLINEWITH(cat('./Base.xaiml'), 'terminal'):
print(line)
最终,你的功能应该是:
with open('thefile') as fin:
matching = (line for line in fin if 'something' in line)
for line in matching:
# do something
或者,将它们全部放入列表中:
lines = list(matching)
答案 2 :(得分:1)
文件对象是可迭代的,列表推导可用于过滤输入。
with open('./Base.xaiml','r') as lines:
matching_lines = [ line.rstrip('\n')
for line in lines
if line.find('terminal') != -1 ]
for line in matching_lines:
print line # change to print(line) for python 3.x
编辑:或者如果你真的希望它成为一个功能
def matching_lines(filename, substring):
""" Return all the lines in a file that contain a given substring. """
with open(filename,'r') as f:
return [s.rstrip('\n') for s in f if s.find(substring) != -1]
for s in matching_lines('./Base.xaiml', 'terminal'):
print s