Question:
(Python)skip first 2 lines in files?
Ornitorinco blu
2018-02-09 21:40:45 UTC
Hello!
Once I read that while reading a file in Python the first two lines of it can be skipped, because they are quite useless. Does this make sense to you? Why should they be not relevant?
Three answers:
Chris
2018-02-09 22:13:56 UTC
That depends entirely on the file in question; if you save a basic Excel table as .csv for instance the first two lines will contain the column names and the first data row, respectively.



It's nonsense to generalize a statement like this (especially a statement as nonsensical), and the fact that you're using Python to read the file has nothing to do with it either.



I'd be interested in where on earth you read that.
husoski
2018-02-09 22:13:09 UTC
They are probably talking about Python source files intended to run as scripts in a Unix-style shell. The first line can be a specialized comment, called a "shebang" line, and any Python source file can have an encoding comment at or near the top. They might look something like:



#!/usr/bin/python

# -*- coding: -*-



...where specifies how non-ASCII characters are encoded in the rest of the source file.



You won't see the first line unless the source is intended to be run from the terminal as a command, and you might not see the second line if no non-ASCII characters are used in the source.



Those two lines aren't useless, since each does something (the first tells the shell what interpreter to run; the second allows using non-ASCII characters in your program) but they aren't part of the program. They are more like metadata.
brilliant_moves
2018-02-09 21:46:41 UTC
While this may have been true for a particular file, this isn't true in the general sense. Can you provide the text of the file in question?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...