Blaise Delaney
Blaise Delaney

Reputation: 321

Read in different amount of columns ignoring specific elements in Python

I am quite new to Python so please forgive my limited knowledge on the matter.

My task is to read each line from the following text file:

4   4738    6208    13891   14714
5   848 1184    3227    6539    7139
5   2748    8697    14917   15168   15751 
3   3568    10845   15435
4   5136    5460    12082   15854
4   3431    4571    10360   12118
0
3   1202    8022    13163
4   2510    2603    7023    8035
3   4886    7131    8428
5   1090    1091    2613    6863    14302  
3   7747    9374    11169
4   1360    2356    5122    11091

However, I'd like to ignore the first element of each row (namely all those 4s,5s,0, and 3s)and read in the rest, storing the numbers into an array.

I tried using the functions numpy.loadtxt, numpy.genfromtxt, but there seems to be a problem with the fact that the number of columns varies. I tried to optimize this by reading up to say, 10 columns and inserting an "N" when there is no number, but i'm wondering if there is a more efficient way of doing things.

Thanks

Blaise

Upvotes: 0

Views: 214

Answers (2)

Jiminion
Jiminion

Reputation: 5168

Read the whole line and then split based on the whitespace. It should return a list of the correct size for each line. You will have to ignore the first element.

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 114005

This should give you all the numbers (except the first column) in a jagged 2D list of lists of ints

with open('path/to/file') as infile:
    allNums = [[int(num) for num in line.strip().split()[1:]] for line in infile]

If you want to turn this into a non-jagged list of lists of ints, then:

import itertools
with open('path/to/file') as infile:
    allNums = [[int(num) for num in line.strip().split()[1:]] for line in infile]
nullValue = None
allNums = list(itertools.izip.from_iterable(allNums, fillvalue=None)) # python 2.x
# allNums = list(itertools.zip.from_iterable(allNums, fillvalue=None)) # python 3.x

Upvotes: 2

Related Questions