Seite 1 von 1

Edit via np.loadtxt dat file

Verfasst: Donnerstag 3. April 2014, 11:04
von gaberchen
Hi guys

This is my data
Results:
Zone 8: -1.402269e-02
Zone 9: -4.560774e-02
Zone 10: -5.631626e-02
Zone 11: -7.443307e-02
Zone 12: -9.302684e-02
Zone 13: -1.117405e-01
Zone 14: -1.306105e-01
Zone 15: -1.792732e-01
Zone 16: -2.314611e-01
Zone 17: -2.887363e-01
Zone 18: -3.559923e-01
Zone 19: -3.887271e-01
Zone 20: -4.277115e-01
Zone 21: -4.819702e-01
Zone 22: -5.834202e-01
Zone 23: -7.132077e-01
Zone 24: -8.713250e-01
Zone 25: -1.011029e+00
Zone 26: -7.359946e-01
Total: -6.794606e+00
I want to load in just the numbers and skip the first and last row. I tried with np.loadtxt('test.dat', delimiter=':', skiprows=1,21) but I failed.

Any suggestion?

Re: Edit via np.loadtxt dat file

Verfasst: Donnerstag 3. April 2014, 11:48
von EyDu
Hi,

your code has several errors. First of all, it has a syntax error. Non-keyword arguments are not allowed after keyword arguments, but that's how the end of your line is interpreted. The 21 is an extra parameter and not part of "skiprows". You have to use a tuple to join them to one argument:

Code: Alles auswählen

np.loadtxt('test.dat', delimiter=':', skiprows=(1,21))
But this leads to the next error. Indexing starts at 0. If you want to skip the first line and line 21, you have to skip index 0 and index 20:

Code: Alles auswählen

np.loadtxt('test.dat', delimiter=':', skiprows=(0,20))
But it doesn't really matter in this case, because the documentation clearly stats:
skiprows : int, optional
Skip the first `skiprows` lines; default: 0.
"skiprows" accepts a single integer only and skips the first "skiprows" lines only. So, slicing is the solution:

Code: Alles auswählen

np.loadtxt('test.dat', delimiter=':')[1:-1]
But you are not finished yet. "np.loadtxt" expects floats, but converting "Zone X" into a float will fail. So you have to add a type signature:

Code: Alles auswählen

np.loadtxt('test.dat', delimiter=':', dtype=(str, float))[1:-1]

Re: Edit via np.loadtxt dat file

Verfasst: Donnerstag 3. April 2014, 11:54
von BlackJack
@gaberchen: The exception would have been interesting. And is quite self explanatory:

Code: Alles auswählen

In [9]: np.loadtxt('test.txt', delimiter=':', skiprows=(1, 21))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-8897c6451080> in <module>()
----> 1 np.loadtxt('test.txt', delimiter=':', skiprows=(1, 21))

/usr/lib/pymodules/python2.7/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack)                           
    669 
    670         # Skip the first `skiprows` lines
--> 671         for i in xrange(skiprows):
    672             fh.readline()
    673 

TypeError: an integer is required
And then looking into the documentation about the `skiprows` argument.

Try the `numpy.genfromtxt()` function which has more arguments and ways to skip rows and select which values to read into the result.

Re: Edit via np.loadtxt dat file

Verfasst: Donnerstag 3. April 2014, 12:38
von gaberchen
Thanks at you guys, I will check the statements this afternoon