我正在从Windows服务器读取各种日志,当前正在抓取文件夹中的最新日志。
然后,我想扫描该日志,只打印包含特定字符串的最后一行。
以下将打印包含字符串的所有行 -
def read_logfile(master_log):
for line in master_log:
if line.contains('[76:Health]:'):
print line
如何让它只打印它找到的最后一个匹配项?
答案 0 :(得分:2)
这样做的简单方法就是每次点击时都存储,并在循环外打印:
var kernel = new StandardKernel();
// Set up kernel
var substitute = Substitute.For<ISubstitutedObject>();
kernel.Bind<ISubstitutedObject>().ToContstant(substitute);
您可以使用collections.deque
与适当的protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
Window.SetSoftInputMode(SoftInput.AdjustResize);
AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
LoadApplication(new App());
}
public class AndroidBug5497WorkaroundForXamarinAndroid
{
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
// CREDIT TO Joseph Johnson (http://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.com
public static void assistActivity(Activity activity)
{
new AndroidBug5497WorkaroundForXamarinAndroid(activity);
}
private Android.Views.View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity)
{
FrameLayout content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
mChildOfContent = content.GetChildAt(0);
ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
vto.GlobalLayout += (object sender, EventArgs e) => {
possiblyResizeChildOfContent();
};
frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious)
{
int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
Rect r = new Rect();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
return (r.Bottom - r.Top);
}
return r.Bottom;
}
}
一起推广到最后def read_logfile(master_log):
lastmatch = None
for line in master_log:
if '[76:Health]:' in line:
lastmatch = line
if lastmatch is not None:
print lastmatch
次匹配,这样您只需n
所有匹配,推出最早的一次你超过了限制。以下工作与上面的代码完全相同,但允许第二个参数打印更多行:
maxlen
答案 1 :(得分:1)
将其存储在数组中,然后打印数组中的最后一项。可以使用pop
将该行作为str
返回。
def read_logfile(master_log):
last_line_holder = []
for line in master_log:
if line.contains('[76:Health]:'):
last_line_holder.append(line)
print(last_line_holder[-1])
答案 2 :(得分:1)
您可以按相反的顺序迭代文件。
for line in reversed(master_log.readlines()):
if '[76:Health]:' in line:
print(line)
break
如果您的文件很小,阅读它不会有问题。如果它很大,请选择其他解决方案。