<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erxin, Shang(Edwin, 尚尔鑫)&#039;s Home Page &#187; Algorithm</title>
	<atom:link href="http://www.shangerxin.com/?cat=26&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.shangerxin.com</link>
	<description>Knowledge should be free.</description>
	<lastBuildDate>Tue, 24 Aug 2021 02:27:23 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.13</generator>
	<item>
		<title>Get required lasts lines from a huge text file in python</title>
		<link>http://www.shangerxin.com/?p=94</link>
		<comments>http://www.shangerxin.com/?p=94#comments</comments>
		<pubDate>Fri, 19 Dec 2014 13:28:30 +0000</pubDate>
		<dc:creator><![CDATA[Shang Erxin]]></dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.shangerxin.com/?p=94</guid>
		<description><![CDATA[During my work, I need to parse the result from some huge log files. The result is always save at the end of the file. So I need find a way to get the required last lines to accelerate the parse speed. Here is the function I have written to achieve this requirement in python. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>During my work, I need to parse the result from some huge log files. The result is always save at the end of the file. So I need find a way to get the required last lines to accelerate the parse speed. Here is the function I have written to achieve this requirement in python.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Get the last lines from a text file</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;font-weight:bold;">def</span> last_lines<span style="color: black;">&#40;</span>filename, lines=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #483d8b;">    return the last lines from the specify files</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #483d8b;">    '</span><span style="color: #483d8b;">''</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> fh:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        fh.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>0, <span style="color: #dc143c;">os</span>.<span style="color: black;">SEEK_END</span><span style="color: black;">&#41;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;font-weight:bold;">if</span> fh.<span style="color: black;">tell</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == 0:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">[</span><span style="color: black;">]</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        fh.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>-2, <span style="color: #dc143c;">os</span>.<span style="color: black;">SEEK_END</span><span style="color: black;">&#41;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #808080; font-style: italic;">#while not BOF</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;font-weight:bold;">while</span> fh.<span style="color: black;">tell</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> 0:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            cur_char = fh.<span style="color: black;">read</span><span style="color: black;">&#40;</span>1<span style="color: black;">&#41;</span>.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #ff7700;font-weight:bold;">if</span> cur_char == <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                lines -= 1</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #ff7700;font-weight:bold;">if</span> lines <span style="color: #66cc66;">&gt;</span> 0:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                fh.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>-2, <span style="color: #dc143c;">os</span>.<span style="color: black;">SEEK_CUR</span><span style="color: black;">&#41;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #ff7700;font-weight:bold;">else</span>:</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">                <span style="color: #ff7700;font-weight:bold;">break</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">[</span>line.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fh.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">]</span></div></li></ol></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shangerxin.com/?feed=rss2&#038;p=94</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
