1

I try to read a .txt file containing 4 columns with names col1, col2, col3 and col4, with data types string, string, float and float.

I just want to read the columns col3 and col4 (for this example).

I used: table = numpy.genfromtxt(filename, skip_header=1, usecols=(2, 3))

Then I figured out that the strings in columns col1 and col2 could be phrases (separated by spaces), then usecols is taking the wrong columns.

Is it possible to get the last n columns ?

Is it possible to read the columns by using specific column name ?

Thanks

Chr
  • 875
  • 1
  • 10
  • 27
mglasner
  • 75
  • 2
  • 9

1 Answers1

2

usecols parameter accepts sequence of negative numbers.

To get last two columns :

table = numpy.genfromtxt(filename, skip_header=1, usecols=(-2, -1))
Chr
  • 875
  • 1
  • 10
  • 27
  • What if some strings has spaces and others don't ?, that's my problem now :/ – mglasner Oct 19 '16 at 16:07
  • With the default white space delimiter spaces in strings are delimiters. So you need some other delimiter, e.g. ','. `genfromtxt` also works with fixed column widths (entered as a delimiter list). – hpaulj Oct 19 '16 at 17:46