我是python编程的新手。我正在处理一个文本文件,它是软件的结果文件。基本上每当我们处理该软件时,它都会将所有消息写入结果文本文件(类似于日志文件)。
现在我的问题是该文件有很多表,如下所示:
it may have some million lines above
* ============================== INERTIA ==============================
* File: /home/hamanda/transfer/cradle_vs30_dkaplus01_fwd_dl140606_fem140704_v00.bif
* Solver: Nastran
* Date: 24/09/14
* Time: 10:29:50
* Text:
*
* Area +1.517220e+06
* Volume +5.852672e+06
*
* Structural mass +4.594348e-02
* MASS elements +0.000000e+00
* NSM on property entry +0.000000e+00
* NSM by parts (VMAGen and MPBalanc) +0.000000e+00
* NSM by NSMCreate +0.000000e+00
* Total mass +4.594348e-02
*
* Center of gravity
* in the global +1.538605e+02 +3.010898e+00 -2.524868e+02
* coordinate system
*
* Moments of inertia +8.346990e+03 +6.187810e-01 +1.653922e+03
* about the global +6.187810e-01 +5.476398e+03 +4.176218e+01
* coordinate system +1.653922e+03 +4.176218e+01 +7.746156e+03
*
* Steiner share +2.929294e+03 +4.016500e+03 +1.088039e+03
*
* Moments of inertia +5.417696e+03 +2.190247e+01 -1.308790e+02
* about the center +2.190247e+01 +1.459898e+03 +6.835397e+00
* of gravity -1.308790e+02 +6.835397e+00 +6.658117e+03
* ---------------------------------------------------------------------
some lines below and this table may repeat if user does any change to area and volume
values.----------
现在我的问题是如何在控制台上打印最新的表格。我能够打印表格的第一次出现,现在我无法获得最新出现的表格。
我需要在控制台上打印最新的表格我该怎么办? 这是我的代码:
input = open(fileName,'r')
intable = False
for line in input:
if line.strip() == "* ============================== INERTIA ==============================":
intable = True
if line.strip() == "* ---------------------------------------------------------------------":
intable = False
break
if intable and line.strip().startswith("*"):
z1=(line.strip())
print(z1)
答案 0 :(得分:1)
试试这个:
f = open(fileName,'r')
content = f.readlines()
content.reverse()
for line in content:
if line.strip() == "* ============================== INERTIA ==============================":
index = content.index(line)
break
for line in content[index::-1]:
print line
答案 1 :(得分:1)
如果你可以使用bash,那么下面是更有效的方式。
RESULT_FILE="result_text_file_name"
START_LINE=$(grep -n "===== INERTIA ====" $RESULT_FILE | tail -1 | cut -d":" -f1)
END_LINE=$(grep -n " --------------" $RESULT_FILE | tail -1 | cut -d":" -f1)
LINE_COUNT=$(wc -l $RESULT_FILE | awk '{print $1}')
tail -n `expr $LINE_COUNT - $FIRST_LINE + 1` $RESULT_FILE | head -n `expr $END_LINE - $FIRST_LINE + 1`
你还想要python,然后阅读帖子How to read lines from a file in python starting from the end
我通过参考上面的页面编写了代码! (以相反的方式读取行)
我假设结果文件是" test.txt"
#!/usr/bin/env python
import sys
import os
import string
"""read a file returning the lines in reverse order for each call of readline()
This actually just reads blocks (4096 bytes by default) of data from the end of
the file and returns last line in an internal buffer. I believe all the corner
cases are handled, but never can be sure..."""
class BackwardsReader:
def readline(self):
while len(self.data) == 1 and ((self.blkcount * self.blksize) < self.size):
self.blkcount = self.blkcount + 1
line = self.data[0]
try:
self.f.seek(-self.blksize * self.blkcount, 2) # read from end of file
self.data = string.split(self.f.read(self.blksize) + line, '\n')
except IOError: # can't seek before the beginning of the file
self.f.seek(0)
self.data = string.split(self.f.read(self.size - (self.blksize * (self.blkcount-1))) + line, '\n')
if len(self.data) == 0:
return ""
# self.data.pop()
# make it compatible with python <= 1.5.1
line = self.data[-1]
self.data = self.data[:-1]
return line + '\n'
def __init__(self, file, blksize=4096):
"""initialize the internal structures"""
# get the file size
self.size = os.stat(file)[6]
# how big of a block to read from the file...
self.blksize = blksize
# how many blocks we've read
self.blkcount = 1
self.f = open(file, 'rb')
# if the file is smaller than the blocksize, read a block,
# otherwise, read the whole thing...
if self.size > self.blksize:
self.f.seek(-self.blksize * self.blkcount, 2) # read from end of file
self.data = string.split(self.f.read(self.blksize), '\n')
# strip the last item if it's empty... a byproduct of the last line having
# a newline at the end of it
if not self.data[-1]:
# self.data.pop()
self.data = self.data[:-1]
if(__name__ == "__main__"):
f = BackwardsReader("test.txt")
end_line = "---------------------------------------------------"
start_line = "========= INERTIA ======="
lines = []
intable = False
line = f.readline()
while line:
if line.find(end_line) >= 0:
intable = True
if intable:
lines.append(line)
if line.find(start_line) >= 0:
break
line = f.readline()
lines.reverse()
print "".join(lines)
和测试结果!
[my server....]$ wc -l test.txt
34008720 test.txt
[my server....]$ time python test.py
* ============================== INERTIA ==============================
* File: /home/hamanda/transfer/cradle_vs30_dkaplus01_fwd_dl140606_fem140704_v00.bif
* Solver: Nastran
* Date: 24/09/14
* Time: 10:29:50
* Text:
*
* Area +1.517220e+06
* Volume +5.852672e+06
*
* Structural mass +4.594348e-02
* MASS elements +0.000000e+00
* NSM on property entry +0.000000e+00
* NSM by parts (VMAGen and MPBalanc) +0.000000e+00
* NSM by NSMCreate +0.000000e+00
* Total mass +4.594348e-02
*
* Center of gravity
* in the global +1.538605e+02 +3.010898e+00 -2.524868e+02
* coordinate system
*
* Moments of inertia +8.346990e+03 +6.187810e-01 +1.653922e+03
* about the global +6.187810e-01 +5.476398e+03 +4.176218e+01
* coordinate system +1.653922e+03 +4.176218e+01 +7.746156e+03
*
* Steiner share +2.929294e+03 +4.016500e+03 +1.088039e+03
*
* Moments of inertia +5.417696e+03 +2.190247e+01 -1.308790e+02
* about the center +2.190247e+01 +1.459898e+03 +6.835397e+00
* of gravity -1.308790e+02 +6.835397e+00 +6.658117e+03
* ---------------------------------------------------------------------
real 0m0.025s
user 0m0.018s
sys 0m0.006
&#13;
答案 2 :(得分:0)
您还可以在列表中捕获文件数据,如下所示:
delimiter = '* ============================== INERTIA ==============================\n'
filedata = open(filepath).read().split(delimiter)
print filedata[-1] # This will print your latest occurrence of table
我不确定代码效率,但绝对有效。 如果需要,您还可以列出表的所有其他事件。