How to Read Specific Lines from a File (By Line Number)

How to read specific lines from a file (by line number)?

If the file to read is big, and you don't want to read the whole file in memory at once:

fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
fp.close()

Note that i == n-1 for the nth line.


In Python 2.6 or later:

with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break

Only read specific line numbers from a large file in Python?

Here are some options:

  1. Go over the file at least once and keep track of the file offsets of the lines you are interested in. This is a good approach if you might be seeking these lines multiple times and the file wont be changed.
  2. Consider changing the data format. For example csv instead of json (see comments).
  3. If you have no other alternative, use the traditional:
def get_lines(..., linenums: list):
with open(...) as f:
for lno, ln in enumerate(f):
if lno in linenums:
yield ln

On a 4GB file this took ~6s for linenums = [n // 4, n // 2, n - 1] where n = lines_in_file.

How to read a specific line using the specific line number from a file in Java?

Unless you have previous knowledge about the lines in the file, there's no way to directly access the 32nd line without reading the 31 previous lines.

That's true for all languages and all modern file systems.

So effectively you'll simply read lines until you've found the 32nd one.

How do I read a specified line in a text file?

.NET 4.0 edit

Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:

string line = File.ReadLines(FileName).Skip(14).Take(1).First();

This will return only the line required


Since you can't predict the location (can you?) of the i-th line in the file, you'll have to read all previous lines too. If the line number is small, this can be more efficient than the ReadAllLines method.

string GetLine(string fileName, int line)
{
using (var sr = new StreamReader(fileName)) {
for (int i = 1; i < line; i++)
sr.ReadLine();
return sr.ReadLine();
}
}

Get specific line from text file using just shell script

sed:

sed '5!d' file

awk:

awk 'NR==5' file

How to read specific lines from a text file a store them as strings. c#

List<string> lines = File.ReadLines(filename).Skip(3).Take(3).ToList();

How to jump to a particular line in a huge text file?

linecache:

The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the traceback module to retrieve source lines for inclusion in the formatted traceback...

Skip specific lines in text file for processing but return these lines as well in the output

You are looping all the lines, and only if the line does not contain a digit or a timestamp like format (for which I think you can just \d to not match a line with a digit) you pass.

But this line at the end censored = profanity.censor(line) overwrites the variable in each iteration, and the method just returns that last overwritten variable.

If you want to keep all lines, you can use a list and add all the lines to it, also the unmodified ones in the parts where you pass, and afterwards return the list, or join the lines on a newline.

The updated part of the code might look like:

result = []
for line in my_transcript.splitlines():
if re.match(timestamp_pattern, line):
result.append(line)
pass
elif re.match(line_num_pattern, line):
result.append(line)
pass
else:
censored = profanity.censor(line)
result.append(censored)
return censored
return "\n".join(result)

Note that you have to escape the dot in the regex to match it literally.

^\d+:\d+:\d+\.\d+


Related Topics



Leave a reply



Submit