How to Delete First Few Rows of .txt File in Python?

Viewed 785

I have a .txt file which looks like:

# Explanatory text
# Explanatory text
# ID_1 ID_2
10310   34426
104510  4582343
1032410 5424233
12410   957422

In the file, the two IDs on the same row are separated with tabs and the tab character is encoded as '\t'

I'm trying to do some analysis using the numbers in the dataset so want to delete the first three rows. How can this be done in Python? I.e. I'd like to produce a new dataset that looks like:

10310   34426
104510  4582343
1032410 5424233
12410   957422

I've tried the following code but it didn't work:

f = open(filename,'r')
lines = f.readlines()[3:]
f.close()

It doesn't work because I get this format (a list, with \t and \n present), not the one I indicated I want above:

[10310\t34426\n', '104510\t4582343\n', '1032410\t5424233\n' ... ]

3 Answers

You Can Try Something Like this

with open(filename,'r') as fh

    for curline in fh:

         # check if the current line
         # starts with "#"

         if curline.startswith("#"):
            ...
            ...
         else:
            ...
            ...

You can use Python's Pandas to do these kind of tasks easily:

import pandas as pd

pd.read_csv(filename, header=None, skiprows=[0, 1, 2], sep='\t')

Ok, here is the solution:

with open('file.txt') as f:
    lines = f.readlines()

lines = lines[3:]

Remove Comments

This function remove all comment lines

def remove_comments(lines):
    return [line for line in lines if line.startswith("#") == False]

Remove n number of top lines

def remove_n_lines_from_top(lines, n):
    if n <= len(lines):
        return lines[n:]
    else:
        return lines

Here is the complete source:

with open('file.txt') as f:
    lines = f.readlines()


def remove_comments(lines):
    return [line for line in lines if line.startswith("#") == False]

def remove_n_line(lines, n):
    return lines[n if n<= len(lines) else 0:]

lines = remove_n_lines_from_top(lines, 3)

f = open("new_file.txt", "w+") # save on new_file
f.writelines(lines)
f.close()
Related