I have a file that contains some numerical data. What I need to do is find the third to last row and need to extract that data out, and put it into another file called newyork.csv.
However, I am having a hard time extracting the 3rd 4th 5th etc row; this is the code that I have. If I would guess I think the problem starts at----- for line in file2:.
How can I get this code to extract the 3rd to last row?
Weather = open("weathernewyork.txt",'r').read().split('\n')
csvholder = []
i = 0
while i < len(Weather):
with open(Weather[i], 'r') as file2:
reader = csv.reader(file2)
wtr__12 = open(Weather[i]).read().split('\n')
new_york_d = wtr__12[-3]
new_york_d = numpy.array(new_york_d)
for line in file2:
line = line.strip()
new_york_d = line.split(",")[4:]
xx = numpy.array(new_york_d).reshape(-1,8)
csvholder.append(xx)
i = i+1
xxz = numpy.array(csvholder).reshape(-1,8)
numpy.savetxt(newyork.csv", xxz, delimiter=",", fmt='%s')
This is how the data file looks like.
0.0002%,3/30/2005,0.205130307,-0.001238007,1,0,0,0,0,1,1,1 <- I want to extract this one
0.0004%,3/31/2005,-0.10252641,-0.010432191,1,0,0,0,1,1,1,1
-0.0009%,4/1/2005,0.101510875,-0.000877706,1,0,0,0,0,1,1,1 <- Python extracts this one which I don't want
UPDATE: I forgot to mention that the file
Weather = open("weathernewyork.txt",'r').read().split('\n')
inside has other files to open. I am looking at all counties.
Weathertwice. Once you read the whole contents into a list, and take the third from last line. But then you overwrite the numpy array you created from that line with each of the other lines you read from the file, in turn. You also have acsv.readeryou never use....