Reputation: 289
I have a txt file of data that I only want to load in the even lines from.
Is there a way to do this in python without using loops?
Here are the first 10 lines of my data file:
1 25544U 98067A 98324.28472222 -.00003657 11563-4 00000+0 0 10
2 25544 51.5908 168.3788 0125362 86.4185 359.7454 16.05064833 05
1 25544U 98067A 98324.33235038 .11839616 11568-4 57349-2 0 28
2 25544 51.6173 168.1099 0123410 88.0187 273.4932 16.04971811 11
1 25544U 98067A 98324.45674522 -.00043259 11566-4 -18040-4 0 32
2 25544 51.5914 167.4317 0125858 91.3429 269.4598 16.05134416 30
1 25544U 98067A 98324.51913017 .00713053 11562-4 34316-3 0 48
2 25544 51.5959 167.1152 0123861 87.8179 273.5890 16.05002967 44
1 25544U 98067A 98324.51913017 .00713053 11562-4 34316-3 0 59
2 25544 51.5959 167.1152 0123861 87.8179 273.5890 16.05002967 44
Upvotes: 1
Views: 3715
Reputation: 289
Here's how I ended up doing it:
import numpy as np
import matplotlib.pyplot as plt
filename = 'zarya2000data.txt'
a = np.genfromtxt(filename)
evens = []
odds = []
N = 8 #30 #5826 #number of lines
for i in range(N): #2913*2
if np.mod(i,2) == 0:
evens.append(a[i,:])
else:
odds.append(a[i,:])
oddsArray = np.asarray(odds)
evensArray = np.asarray(evens)
print 'evensArray', evensArray
print''
print 'oddsArray', oddsArray
print ''
Upvotes: 0
Reputation: 107
The np.loadtxt() doesn't have the ability to skip lines unless they are the first N lines. Otherwise you will want to use np.genfromtxt():
with open(filename) as f:
iter = (line for line in f if is_even_line(line))
data = np.genfromtxt(iter)
where is_even_line() is a function that returns a boolean, if the given line is even. In your case, since the first column indicates whether the line is odd or even, is_even_line() could look like this:
def is_even_line(line):
return line[0] == '2'
Upvotes: 1
Reputation: 4744
One way to do it is to use a counter and modulo operator:
fname = 'load_even.txt'
data = [];
cnt = 1;
with open(fname, 'r') as infile:
for line in infile:
if cnt%2 == 0:
data.append(line)
cnt+=1
This reads the file line by line, increasing the counter cnt
after each line, and appending that line to data
only if the counter value is even which in this case corresponds to an even line number.
For a particular case of numpy
array you can use this:
import numpy as np
fname = 'load_even.txt'
data = [];
cnt = 1;
with open(fname, 'r') as infile:
for line in infile:
if cnt%2 == 0:
data.append(line.split())
cnt+=1
data = np.asarray(data, dtype = float)
Upvotes: 2