Is there a shorter (perhaps more pythonic) way of opening a text file and reading past the lines that start with a comment character?
In other words, a neater way of doing this
fin = open("data.txt")
line = fin.readline()
while line.startswith("#"):
line = fin.readline()
This method uses next () to skip the header and starts reading the file from line 2. Note: If you want to print the header later, instead of next (f) use f.readline () and store it as a variable or use header_line = next (f). This shows that the header of the file is stored in next ().
This method uses readlines () to skip the header and starts reading the file from line 2. readlines () uses the slicing technique. As you can see in the below example, readlines [1:], it denotes that the reading of the file starts from index 1 as it skips the index 0. This is a much more powerful solution as it generalizes to any line.
Python is a very powerful programming language. Let's see how to skip a line in Python. It is very easy. I love Python. It makes everything so fun. Python is a very powerful programming language. Let's see how to skip a line in Python. It is very easy. I love Python. It makes everything so fun. Let’s now skip the 3rd line. This is a sample file.
In Python 3, PEP 3132 has introduced a new method of extended unpacking: If you need to assign something (for instance, in unpacking), but will not need that variable, use __: filename = 'foobar.txt' basename, __, ext = filename.rpartition ('.')
At this stage in my arc of learning Python, I find this most Pythonic:
def iscomment(s):
return s.startswith('#')
from itertools import dropwhile
with open(filename, 'r') as f:
for line in dropwhile(iscomment, f):
# do something with line
to skip all of the lines at the top of the file starting with #
. To skip all lines starting with #
:
from itertools import ifilterfalse
with open(filename, 'r') as f:
for line in ifilterfalse(iscomment, f):
# do something with line
That's almost all about readability for me; functionally there's almost no difference between:
for line in ifilterfalse(iscomment, f))
and
for line in (x for x in f if not x.startswith('#'))
Breaking out the test into its own function makes the intent of the code a little clearer; it also means that if your definition of a comment changes you have one place to change it.
for line in open('data.txt'):
if line.startswith('#'):
continue
# work with line
of course, if your commented lines are only at the beginning of the file, you might use some optimisations.
from itertools import dropwhile
for line in dropwhile(lambda line: line.startswith('#'), file('data.txt')):
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With