Read file from line 2 or skip header row

PythonFile Io

Python Problem Overview


How can I skip the header row and start reading a file from line2?

Python Solutions


Solution 1 - Python

with open(fname) as f:
    next(f)
    for line in f:
        #do something

Solution 2 - Python

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

Solution 3 - Python

If you want the first line and then you want to perform some operation on file this code will helpful.

with open(filename , 'r') as f:
    first_line = f.readline()
    for line in f:
			# Perform some operations

Solution 4 - Python

If slicing could work on iterators...

from itertools import islice
with open(fname) as f:
    for line in islice(f, 1, None):
        pass

Solution 5 - Python

f = open(fname).readlines()
firstLine = f.pop(0) #removes the first line
for line in f:
    ...

Solution 6 - Python

To generalize the task of reading multiple header lines and to improve readability I'd use method extraction. Suppose you wanted to tokenize the first three lines of coordinates.txt to use as header information.

Example

coordinates.txt
---------------
Name,Longitude,Latitude,Elevation, Comments
String, Decimal Deg., Decimal Deg., Meters, String
Euler's Town,7.58857,47.559537,0, "Blah"
Faneuil Hall,-71.054773,42.360217,0
Yellowstone National Park,-110.588455,44.427963,0

Then method extraction allows you to specify what you want to do with the header information (in this example we simply tokenize the header lines based on the comma and return it as a list but there's room to do much more).

def __readheader(filehandle, numberheaderlines=1):
    """Reads the specified number of lines and returns the comma-delimited 
    strings on each line as a list"""
    for _ in range(numberheaderlines):
        yield map(str.strip, filehandle.readline().strip().split(','))
        
with open('coordinates.txt', 'r') as rh:
    # Single header line
    #print next(__readheader(rh))

    # Multiple header lines
    for headerline in __readheader(rh, numberheaderlines=2):
        print headerline  # Or do other stuff with headerline tokens

Output

['Name', 'Longitude', 'Latitude', 'Elevation', 'Comments']
['String', 'Decimal Deg.', 'Decimal Deg.', 'Meters', 'String']

If coordinates.txt contains another headerline, simply change numberheaderlines. Best of all, it's clear what __readheader(rh, numberheaderlines=2) is doing and we avoid the ambiguity of having to figure out or comment on why author of the the accepted answer uses next() in his code.

Solution 7 - Python

If you want to read multiple CSV files starting from line 2, this works like a charm

for files in csv_file_list:
        with open(files, 'r') as r: 
            next(r)                  #skip headers             
            rr = csv.reader(r)
            for row in rr:
                #do something

(this is part of Parfait's answer to a different question)

Solution 8 - Python

# Open a connection to the file
with open('world_dev_ind.csv') as file:

    # Skip the column names
    file.readline()

    # Initialize an empty dictionary: counts_dict
    counts_dict = {}

    # Process only the first 1000 rows
    for j in range(0, 1000):

        # Split the current line into a list: line
        line = file.readline().split(',')

        # Get the value for the first column: first_col
        first_col = line[0]

        # If the column value is in the dict, increment its value
        if first_col in counts_dict.keys():
            counts_dict[first_col] += 1

        # Else, add to the dict and set value to 1
        else:
            counts_dict[first_col] = 1

# Print the resulting dictionary
print(counts_dict)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsuper9View Question on Stackoverflow
Solution 1 - PythonSilentGhostView Answer on Stackoverflow
Solution 2 - PythonchriscauleyView Answer on Stackoverflow
Solution 3 - Pythonsaimadhu.polamuriView Answer on Stackoverflow
Solution 4 - PythonVajk HermeczView Answer on Stackoverflow
Solution 5 - PythonDror HilmanView Answer on Stackoverflow
Solution 6 - PythonMinh TranView Answer on Stackoverflow
Solution 7 - PythonTiago Martins PeresView Answer on Stackoverflow
Solution 8 - PythonMauro RementeriaView Answer on Stackoverflow