Justin Brousseau Justin Brousseau - 6 months ago 8
Python Question

When reading in a txt matrix, how can i skip first column

I have a file that looks like this:

1 2 3 4 5 6 7
1 0 1 1 1 1 1 1
2 0 0 1 1 1 1 1
3 0 0 0 1 1 1 1
4 0 0 0 0 1 1 1
5 0 0 0 0 0 1 1
6 0 0 0 0 0 0 1
7 0 0 0 0 0 0 0


I want to read in only the 1 and 0 and ignore the top header row and the row name (the first column).

So far I have the header line all set, but how can I skip the skip column. My code so far

with open('file') as f:
next(f) #skips header row
content = [x.strip('\n') for x in f.readlines()]


I'm trying to use only base python and no libraries.

Answer

Use a simple indexing:

with open('file') as f:
    next(f)
    content = [x.strip().split()[1:] for x in f]

This will give you the splitted zero and ones as a nested list.

If you don't want to split the lines you can still use indexing in order to remove the first character.

content = [x[1:].strip() for x in f]

Or as a Numpythonic approach you can use loadtxt() function:

>>> import numpy as np
>>> form io import StringIO
>>> np.loadtxt(StringIO(my_string), skiprows=1)[:,1:]
array([[ 0.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])