python - What is the most efficient way to get first and last line of a text file? -
I have a text file that contains a time stamp on each line, my goal is to find time slots all the time in order Therefore, the first line will be the earliest time and the last line will be the latest time. I just need the first and very last line, what would be the most effective way of getting these rows in Python?
Note: These files are relatively large in length, each have about 1-2 million lines and I have to do this for several hundred files
with fname ('fname', 'rb') in the form of FH: first = next (FH). DCDA (FH.Sec (-1024, 2) final = FH.Redline () [- 1] .decode ()
Here the variable value is 1024: This represents the average string length Does. I choose 1024 only for example. If you have an average line length estimate, you can use that value time 2.
Since you have no information about the upper limit for the length of the line, the obvious solution will be above the loop file:
for the line in the file: pass = Line
You do not have to bother with the binary flag, which you simply use Open (FNA)
.
ETA : Since you have a lot of files to work with, so you random Run this code to set the length of the sample
and the last row. With a major value of position change (say 1 MB) with great value This will help you estimate the value of the full run.
Comments
Post a Comment