How to skip initial comment lines using if statementĪ naive way to read a file and skip initial comment lines is to use “if” statement and check if each line starts with the comment character “#”. However, the third approach, which uses itertools’ dropwhile to skip lines while reading a file line by line is efficient and elegant. The second approach to skip lines while reading a text file is efficient, but still bit clunky and a bit of a hack. The first approach is a naive approach using if statement and not efficient. You don’t have to know how many lines you want to skip. Here are 3 ways to read a text file line by line Python and skip initial comment lines. The initial few lines of the text file that you want to skip are typically comment or some meta data and starts with some special characters like “#”. Often, you are not interested in initial few lines and want to skip them and work with rest of the file. Reading a text file line by line is one of the common activities you do while dealing with a big text file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |